Threads question (newbie)


(Shashank Date) #1

This is using Ruby 1.6.7 on Windows (mswin32).

The following program without the sleep(0.01) stmt
seems to hang/stall after creating 3 threads until one thread is done.
Thus it takes over 35 seconds to finish.

But with the sleep stmt it starts all the 10 threads concurrently and the
entire
process is over in about 10 to 12 seconds as expected.

What am I missing ?
TIA,

– Shanko

···

require ‘thread’

commands = []
10.times { |i|
commands << “ping 1.1.1.1 -n 10 -w 1000 > junk#{i}.txt”
}
puts commands,"-------------",echo %TIME%

threads = []
for command in commands
threads << Thread.new(command){ |myCmd|
sleep(0.01); # try commenting this out
#{myCmd}
}
end

threads.each { |aThread| aThread.join }
puts echo %TIME%


(Nobuyoshi Nakada) #2

Hi,

This is using Ruby 1.6.7 on Windows (mswin32).

The following program without the sleep(0.01) stmt
seems to hang/stall after creating 3 threads until one thread is done.
Thus it takes over 35 seconds to finish.

IO multiplexing works with only sockets in mswin32 – this
comes from winsock limitation. And new thread would get the
control immediatly, so #{myCmd} blocks before the main thread
would run again.

But with the sleep stmt it starts all the 10 threads concurrently and the
entire
process is over in about 10 to 12 seconds as expected.

In that case, the main thread gets the control before the
backticks. And invoked commands can run simultaneously.

With Thread.stop/wakeup it may also work.

threads = []
for command in commands
threads << Thread.new(command){ |myCmd|
Thread.stop
#{myCmd}
}
end
Thread.exclusive {threads.each {|aThread| aThread.wakeup}}

···

At Wed, 12 Jun 2002 11:21:17 +0900, Shashank Date wrote:

threads.each { |aThread| aThread.join }
puts echo %TIME%


Nobu Nakada


(Shashank Date) #3

Ok, then, replace the block

10.times { |i|
commands << “ping 1.1.1.1 -n 10 -w 1000 > junk#{i}.txt”
}

with the block:

3.times { |i|
commands << "dir /s > nul"
commands << "ping 1.1.1.1 -n 10 -w 1000 > nul"
commands << “find “nokada” %windir%\. > nul”
}

and you will get the same results !!
But to get all the 9 threads to start simultaneously, I had to bump
sleep(0.01) to sleep(1).

I guess, I did not understand the dependency on “winsock limitation”.

I am sure, we will be able to repeat the experiment without the ping (or any
commands dependant on winsock);
Thanks,
– Shanko

nobu.nokada@softhome.net wrote in message
news:200206120424.g5C4O9214711@sharui.nakada.kanuma.tochigi.jp

Hi,

This is using Ruby 1.6.7 on Windows (mswin32).

The following program without the sleep(0.01) stmt
seems to hang/stall after creating 3 threads until one thread is done.
Thus it takes over 35 seconds to finish.

IO multiplexing works with only sockets in mswin32 – this
comes from winsock limitation. And new thread would get the
control immediatly, so #{myCmd} blocks before the main thread
would run again.

But with the sleep stmt it starts all the 10 threads concurrently and
the

···

At Wed, 12 Jun 2002 11:21:17 +0900, > Shashank Date wrote:

entire
process is over in about 10 to 12 seconds as expected.

In that case, the main thread gets the control before the
backticks. And invoked commands can run simultaneously.

With Thread.stop/wakeup it may also work.

threads = []
for command in commands
threads << Thread.new(command){ |myCmd|
Thread.stop
#{myCmd}
}
end
Thread.exclusive {threads.each {|aThread| aThread.wakeup}}
threads.each { |aThread| aThread.join }
puts echo %TIME%


Nobu Nakada


(Nobuyoshi Nakada) #4

Hi,

···

At Wed, 12 Jun 2002 15:01:29 +0900, Shashank Date wrote:

I am sure, we will be able to repeat the experiment without the ping (or any
commands dependant on winsock);

As you noted, It doesn’t depend on wheather the commands use
winsock or not, I meant the winsock facility used by current
ruby implementation.


Nobu Nakada


(Shashank Date) #5

Got it !

Thanks …

nobu.nokada@softhome.net wrote in message
news:200206130437.g5D4b9200898@sharui.nakada.kanuma.tochigi.jp

Hi,

I am sure, we will be able to repeat the experiment without the ping (or
any

···

At Wed, 12 Jun 2002 15:01:29 +0900, > Shashank Date wrote:

commands dependant on winsock);

As you noted, It doesn’t depend on wheather the commands use
winsock or not, I meant the winsock facility used by current
ruby implementation.


Nobu Nakada