[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Hello, and a question about finding a defined Class from C

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


1 Answer

Joel VanderWerf

2/18/2006 11:33:00 PM

0

Jacob Repp wrote:
> I followed up and it seems that you're right, I had assumed the recv
> was blocking the whole process but it was not. Based on what I've seen
> after some further testing it seems that 'Kernel#gets' and
> 'Socket#connect' can both block the process. Is this correct?

Yes, unfortunately, AFAIK. These are known problems on windows.

Search for "gets windows block sockets" on comp.lang.ruby:

http://groups.google.com/group/comp.lang.ruby/search?group=comp.lang.ruby&q=gets+windows+block+sockets&qt_g=1&searchnow=Search+...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407