[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: TCPSocket.new blocks other threads

e

1/23/2005 10:44:00 PM

> Lähettäjä: "christoph.heindl@gmail.com" <christoph.heindl@gmail.com>
> Aihe: Re: TCPSocket.new blocks other threads
>
> robert,
>
> i could not find any information in ruby-talk...
> :(

I'm trying this:

# test.rb
if $0 == __FILE__
require 'socket'
a = Thread.new do
0.upto 50_000 do |i|
puts i
end
end
a.join
b = Thread.new do
TCPSocket.new 'www.sourceforge.com', 80
end
b.join
end

There's no delay at any point. If I give a nonexistent address,
I get an exception report once the other thread is finished. This
is Ruby 1.8.1 i386-mswin32 on an early XP machine (no SP2) connecting
through cable.

> christoph

E



1 Answer

christoph.heindl@gmail.com

1/24/2005 7:01:00 AM

0

but you are joining the first thread (a)... that means thread b won't
start before thread a terminates.
try this one:

require 'socket'
require 'resolv-replace'

count = 0

b = Thread.new do
begin
Thread.pass
s = TCPSocket.new("192.168.0.5", 6667)
rescue Exception
puts "cannot connect"
end
end

loop {
count += 1
puts count
sleep(0.1)
}

assuming that 192.168.0.5 is not reachable the output in linux will be:
1
2
3
[...]
not connected
[...]
under windows:

1
not connected (after ~20secs)
2
3
4
[...]

christoph