#start threads
threads = []
for i in 1…($number)
threads << Thread.new(i) { |i|
sleep (rand(10))
while(1)
t = TCPSocket.new($host,$port)
sleep (rand(10))
t.close
sleep (rand(10))
end
}
end
print "Q to quit\n"
while(!($stdin.gets =~ /Q.*/))
end
I think the problem here is that you’re getting some sort of exception
raised inside the thread (possibly a connection refused), and aren’t
seeing it.
I would suggest putting this at the top of your program:
Thread.abort_on_exception=true
And see what sort of error you get. Without that, if there’s an exception
in your thread that isn’t rescued, your thread will silently die.
···
On Fri, 12 Dec 2003, JB wrote:
I have soe problems whitn treads and I/O.
My program starts severals threads that open tcp connections.
The main thread waits the user to press Q to stop the program.
It seems that the gets fonction blocks all my threads !!!
How is it possible?
What did I wrong?
#start threads
threads =
for i in 1…($number)
threads << Thread.new(i) { |i|
sleep (rand(10))
while(1)
t = TCPSocket.new($host,$port)
sleep (rand(10))
t.close
sleep (rand(10))
end
}
end
print “Q to quit\n”
while(!($stdin.gets =~ /Q.*/))
end
“If you’ve got a 5000-line JSP page that has “all in one” support
for three input forms and four follow-up screens, all controlled
by “if” statements in scriptlets, well … please don’t show it
to me :-). Its almost dinner time, and I don’t want to lose my
appetite :-).”
- Craig R. McClanahan
Threads and Processes
"And if some thread happens to make a call to the operating system that
takes a long time to complete, all threads will hang until the interpreter
gets control back. "
This is what hapens when I call $stdin.gets
Here is a simplier program to show the problem
require 'thread’
mutex = Mutex.new
$i = 0
thread1 = Thread.new() {
while(1)
mutex.synchronize do
$i = $i + 1
end
end
}
loop = true
print "Q to quit\n"
while(loop)
c= $stdin.gets
loop = false if (c=~ /Q.*/)
mutex.synchronize do
print “i=”,$i,"\n"
$i = 0
end
end