# Threads question (newbie)

**URL:** <https://rubytalk.org/t/threads-question-newbie/219>\
**Category:** ruby-talk\
**Created:** [12 June 2002 02:21 UTC](https://rubytalk.org/t/threads-question-newbie/219 "2002-06-12T02:21:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Date](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Shashank\_Date](https://rubytalk.org/u/Shashank_Date)\
**Post date:** [12 June 2002 02:21 UTC](https://rubytalk.org/t/threads-question-newbie/219/1 "2002-06-12T02:21:17Z")

</div>

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%`

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [12 June 2002 04:24 UTC](https://rubytalk.org/t/threads-question-newbie/219/2 "2002-06-12T04:24:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shashank\_Date](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Shashank\_Date](https://rubytalk.org/u/Shashank_Date)\
**Post date:** [12 June 2002 06:01 UTC](https://rubytalk.org/t/threads-question-newbie/219/3 "2002-06-12T06:01:29Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [13 June 2002 04:37 UTC](https://rubytalk.org/t/threads-question-newbie/219/4 "2002-06-13T04:37:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shashank\_Date](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Shashank\_Date](https://rubytalk.org/u/Shashank_Date)\
**Post date:** [13 June 2002 13:04 UTC](https://rubytalk.org/t/threads-question-newbie/219/5 "2002-06-13T13:04:07Z")

</div>

Got it !

Thanks …

[nobu.nokada@softhome.net](mailto: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
