[Threads + Sockets] Strange behavior

Hi,

I attached a really simple and short script. If you run it, it works as
expected.

If you comment the bottom and uncomment the part directly above, however, it
does not work as expected anymore, it does not execute the block of code
simultanously 10 times anymore - it waits until one socket is finished and
then another thread continues execution.

It seems the socket is not local to the block of code - that maybe why the
other threads don’t touch it until it is closed (when the block of code is
finished) → maybe they all share one socket, but I do not think that is
what is happening.

So if the socket is not local to the block of code, how can I make it local?

Thanks for any help in advance.

Ciao,

-Lars

weird.rb (700 Bytes)

I modified your code to include a rescue clause and of course got a
whole load of…

start
failed
done

Then I set the socket to a valid address and got a whole load of

start
done

So it would seem that your code works but perhaps too fast between the
socket open and close for the other thread to get started. So I added a
sleep 5 between the open and close.

start
start
start
start
start
start
start
start
start
start
done
done
done
done
done
done
done
done
done
done

So the problem with your code is that the operation of opening the
socket and closing it is too fast to allow another thread to get started
giving the appearance of serial execution.

You are learning fast!

I modified your code to include a rescue clause and of course got a
whole load of…

start
failed
done

Then I set the socket to a valid address and got a whole load of

start
done

So it would seem that your code works but perhaps too fast between the
socket open and close for the other thread to get started. So I added a
sleep 5 between the open and close.

Thread.pass may work as well.

···

Peter Hickman (peter@semantico.com) wrote:

So the problem with your code is that the operation of opening the
socket and closing it is too fast to allow another thread to get started
giving the appearance of serial execution.

You are learning fast!


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Excellent! Thanks so much! I would have never guessed that it is too fast to
allow another thread to get started.

Thanks again,

-Lars

···

----- Original Message -----
From: “Peter Hickman” peter@semantico.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, January 21, 2003 5:58 PM
Subject: Re: [Threads + Sockets] Strange behavior

I modified your code to include a rescue clause and of course got a
whole load of…

start
failed
done

Then I set the socket to a valid address and got a whole load of

start
done

So it would seem that your code works but perhaps too fast between the
socket open and close for the other thread to get started. So I added a
sleep 5 between the open and close.

start
start
start
start
start
start
start
start
start
start
done
done
done
done
done
done
done
done
done
done

So the problem with your code is that the operation of opening the
socket and closing it is too fast to allow another thread to get started
giving the appearance of serial execution.

You are learning fast!