Joel VanderWerf
2/18/2006 9:49:00 PM
Jacob Repp wrote:
> It's not that simple. For example:
>
> # server
> server = Thread.new {
> s = UDPSocket.open
> s.bind(nil, 5000)
> while(true)
> data, cli = s.recvfrom(256)
> puts "Server got #{data} from #{cli}"
> Thread.pass
> end
> }
> client = Thread.new {
> s = UDPSocket.open
> s.connect('localhost', 5000)
> while(true)
> s.send("hello", 0)
> Thread.pass
> end
> }
>
> The problem occurs on the first call that the sever thread makes to
> recvfrom(). This is a blocking call and since ruby is actually a
> single threaded process (with virtual soft threads) this hangs all of
> the other virtual threads. So for my test app which is trying to run a
> simulation thread as well as handle socket and console IO this becomes
> a problem quickly.
That code works fine for me, using ruby-1.8.4, both linux and windows
(the one-click msvc-built version).
Also, the Thread.pass is not needed.
I did add a line at the end:
sleep
(You could also use Thread.join(client) or similar.)
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407