Creating and Executing New Threads

D

Dan King

I expected the script below to do the following:

(1) spawn a new thread
(2) connect to a server
(3) read the data transmitted by the server
(4) close the connection.
(5) repeat 1-4

While the script works, it does NOT spawn a new thread for each
connection. Does anyone why that is the case? Thanks.

require 'socket'

threads = []

500.times do
threads << Thread.new do
socket = TCPSocket.open('127.0.0.1', 23)
pid = Process.pid
while line = socket.gets
print "Client thread (#{pid})\t" + line.chomp + "\n"
end
socket.close
end
end

threads.each { |t| t.join }
 
J

Joel VanderWerf

I expected the script below to do the following:

(1) spawn a new thread
(2) connect to a server
(3) read the data transmitted by the server
(4) close the connection.
(5) repeat 1-4

While the script works, it does NOT spawn a new thread for each
connection. Does anyone why that is the case? Thanks.

require 'socket'

threads = []

500.times do
threads<< Thread.new do
socket = TCPSocket.open('127.0.0.1', 23)
pid = Process.pid
while line = socket.gets
print "Client thread (#{pid})\t" + line.chomp + "\n"
end
socket.close
end
end

threads.each { |t| t.join }

All the threads are running in one process, hence there is only one pid.
 
7

7stud --

Dan King wrote in post #998095:
I expected the script below to do the following:

(1) spawn a new thread
(2) connect to a server
(3) read the data transmitted by the server
(4) close the connection.
(5) repeat 1-4

While the script works, it does NOT spawn a new thread for each
connection. Does anyone why that is the case? Thanks.


threads = []

10.times do |i|
threads << Thread.new(i) do |thread_num|
sleep(rand(3))
puts thread_num
end
end

threads.each { |t| t.join }

--output:--
1
2
3
6
5
4
9
0
7
8


Read the first paragraph here:

http://rubylearning.com/satishtalim/ruby_threads.html
 
C

Christopher Dicely


Of course, that paragraph is only correct for MRI 1.8.x and earlier,
and is irrelevant to the issue here, which was correctly addressed in
the first post in the thread, and has nothing to do with any special
peculiarities of thread implementations in any of the various Ruby
implementations, but just with the fact that threads (even native
threads) aren't the same thing as native processes.
 
7

7stud --

Christopher Dicely wrote in post #998130:
Of course, that paragraph is only correct for MRI 1.8.x and earlier,
and is irrelevant to the issue here, which was correctly addressed in
the first post in the thread,

Well...since that link explains that ruby threads all run in a single
process (as well as providing some other pertinent information), and the
first post also states that ruby threads all run in a single process,
the link is highly relevant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top