Re: Sleep in a multithreaded environment



On Mon, Jun 30, 2008 at 1:07 PM, Me Me <emanuelef@xxxxxxxxxx> wrote:
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
--
Posted via 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

.



Relevant Pages

  • Sleep in a multithreaded environment
    ... I'm trying to control an external process I'm running and quit it in ... if it doesn't update it for a while I'll quit it. ... main thread blocks everything. ... puts "DEAD" ...
    (comp.lang.ruby)
  • Re: TCPSocket and RFC 821
    ... generated by the gets call you make right after sending QUIT. ... Try the same program with a mail server located on a different machine ... After sending QUIT on an SMTP ... puts t.gets ...
    (comp.lang.ruby)
  • Re: Goodbye, robbb
    ... >person puts off or doesn't quit because they've lurked the site and seen all ... >hell break loose, then that is sad and i think thoughtless of those ...
    (alt.support.stop-smoking)
  • Re: Goodbye, robbb
    ... person puts off or doesn't quit because they've lurked the site and seen all hell break loose, then that is sad and i think thoughtless of those involved. ...
    (alt.support.stop-smoking)
  • Re: exec race problem
    ... puts $pipe "run all" ... puts $pipe "quit" ... set pipe [open |foo r+] ...
    (comp.lang.tcl)