Threads and io

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?

Thanks alot.

there is my programm

require ‘socket’

#parametrers
$host = $[0]
$port = $
[1]
$number = $*[2]
$number = 1 if ($number == nil)
$number = $number.to_i

#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’re working on windows, that is a known issue I’m afraid :frowning:
I remember this was related to windows select() maybe…

···

il Fri, 12 Dec 2003 15:28:53 +0100, “JB” temuphaey0@jetable.net ha scritto::

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?

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?

Tell us about what you are trying to accomplish?
make a server? or a client?

Its seems to me, that you are confusing server/client.
B: multiple threads which operates on the same port (server).

Take a look at the TCPServer class, here:
http://www.rubycentral.com/book/lib_network.html

···

On Fri, 12 Dec 2003 15:28:53 +0100, JB wrote:
A: using TCPSocket, is a client thing.


Simon Strandgaard

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?

Thanks alot.

there is my programm

require ‘socket’

#parametrers
$host = $[0]
$port = $
[1]
$number = $*[2]
$number = 1 if ($number == nil)
$number = $number.to_i

#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

Derek Lewis

===================================================================
Java Web-Application Developer

  Email    : email@lewisd.com
  Cellular : 604.312.2846
  Website  : http://www.lewisd.com

“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

Extract of Ruby manual:

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