Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4294 > unrolled thread

Creating and Executing New Threads

Started byDan King <dan.king106@yahoo.com>
First post2011-05-11 15:32 -0500
Last post2011-05-12 12:29 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  Creating and Executing New Threads Dan King <dan.king106@yahoo.com> - 2011-05-11 15:32 -0500
    Re: Creating and Executing New Threads Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-11 15:44 -0500
    Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 17:52 -0500
      Re: Creating and Executing New Threads Christopher Dicely <cmdicely@gmail.com> - 2011-05-11 19:42 -0500
        Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 12:29 -0500

#4294 — Creating and Executing New Threads

FromDan King <dan.king106@yahoo.com>
Date2011-05-11 15:32 -0500
SubjectCreating and Executing New Threads
Message-ID<2fad4e571f05e17ee09ba2e4b856b9f4@ruby-forum.com>
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 }

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4296

FromJoel VanderWerf <joelvanderwerf@gmail.com>
Date2011-05-11 15:44 -0500
Message-ID<4DCAF500.8040505@gmail.com>
In reply to#4294
On 05/11/2011 01:32 PM, Dan King wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#4306

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 17:52 -0500
Message-ID<5e9dd109dd9aac91b7eafea052658d81@ruby-forum.com>
In reply to#4294
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4311

FromChristopher Dicely <cmdicely@gmail.com>
Date2011-05-11 19:42 -0500
Message-ID<BANLkTim9pWUBzi04Z10LvvvD362nA-CD4w@mail.gmail.com>
In reply to#4306
On Wed, May 11, 2011 at 3:52 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> Read the first paragraph here:
>
> http://rubylearning.com/satishtalim/ruby_threads.html

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.

[toc] | [prev] | [next] | [standalone]


#4396

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-12 12:29 -0500
Message-ID<efc2747f8a12f06be890c22b8f56eb59@ruby-forum.com>
In reply to#4311
Christopher Dicely wrote in post #998130:
> On Wed, May 11, 2011 at 3:52 PM, 7stud -- <bbxx789_05ss@yahoo.com>
> wrote:
>
>> Read the first paragraph here:
>>
>> http://rubylearning.com/satishtalim/ruby_threads.html
>
> 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.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web