TCPSocket.new blocks other threads

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...
:frowning:

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

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