# Sleep in a multithreaded environment

**URL:** <https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749>\
**Category:** Ruby\
**Created:** [June 30, 2008, 11:09am UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749 "2008-06-30T11:09:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 11:09am UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/1 "2008-06-30T11:09:47Z")

</div>

Hi,  
I’m trying to control an external process I’m running and quit it in  
case it got  
stuck.  
To do that I have to check that the external exe keeps writing on  
stdout, if it doesn’t update it for a while I’ll quit it.  
I came up with the following code (using threads) but the sleep in the  
main thread blocks everything. (The same code works under Mac OSX)  
Is there something I’m doing wrong?  
Or a better way to achieve what I need?  
Thanks a lot in advance

n = 0  
t1 = Thread.new  
{  
begin  
IO.popen(“executable.exe”) do |pipe|  
pipe.each("\r") do |line|  
puts line  
n += 1  
end  
end  
end  
}

k = 0  
while t1.alive?  
sleep(10) # DOESN’T SEEM TO WORK ON WINDOWS  
if k == n  
puts “DEAD”  
t1.raise(“close”)  
end  
k = n  
end

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 11:38am UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/2 "2008-06-30T11:38:45Z")

</div>

On Mon, Jun 30, 2008 at 1:07 PM, Me Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks a lot in advance end k = n end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

pipe=IO.popen(“executable.exe”)  
io=select([io],nil,nil,10) #10 - timeout  
if io==nil  
puts “DEAD”  
t1.raise(“close”)  
end

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 11:48am UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/3 "2008-06-30T11:48:17Z")

</div>

Zhukov P. wrote:

> On Mon, Jun 30, 2008 at 1:07 PM, Me Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## Thanks a lot in advance end k = n end
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> pipe=IO.popen(“executable.exe”)  
> io=select([io],nil,nil,10) #10 - timeout  
> if io==nil  
> puts “DEAD”  
> t1.raise(“close”)  
> end

Hi,  
thanks for answering, I tried your code but I get:  
test\_th.rb:20:in `select’: can’t convert nil into IO (TypeError)

Besides it’s not clear to me what is done by the select.  
Do I still need the thread t1 then?  
Bye

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/4 "2008-06-30T12:56:53Z")

</div>

Hi,

I tried this:

pipe=IO.popen(“hang.exe”)  
io=select([pipe],nil,nil,10) #10 - timeout  
if io==nil  
puts “DEAD”  
end

with hang.exe a process that just hangs.  
It doesn’t seem that the process is launched at all, and it goes on  
without waiting.

Basically I just need to parse an executable output line by line and  
trigger an exception if I don’t get any line after a certain amount of  
time.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/5 "2008-06-30T12:11:38Z")

</div>

On Mon, Jun 30, 2008 at 1:45 PM, Me Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> ## Besides it’s not clear to me what is done by the select. Do I still need the thread t1 then? Bye
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

oh, sorry.: io=select([pipe],nil,nil,10)

no, you does’t, just add code if you want to catch exeption there

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [June 30, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/sleep-in-a-multithreaded-environment/141749/6 "2008-06-30T13:16:59Z")

</div>

Me Me wrote:

> Hi,
> 
> I tried this:
> 
> pipe=IO.popen(“hang.exe”)  
> io=select([pipe],nil,nil,10) #10 - timeout  
> if io==nil  
> puts “DEAD”  
> end
> 
> with hang.exe a process that just hangs.  
> It doesn’t seem that the process is launched at all, and it goes on  
> without waiting.

Actually I checked and the process is running but it became a zombie.  
It seems that the select doesn’t wait
