C extension and Ruby threads

J

John Bob

Hi all,
I've found something a bit weird while trying to make a C extension
wrapping a custom network library :
- My extension creates a BaseClient class with a method 'receive' (it
basically wait a response from a server for a given period of time)
- I created a ruby class Client that inherits from BaseClient. In the
constructor of this class, I create a thread that loops on the method
receive(). Note that in my C code, the receive function does a
WaitForSingleObject (Windows equivalent of pthread_cond_wait ) with a
timeout of 100ms

My problem is that even if my reception thread is in a wait state, my
main thread doesn't run smoothly (it freezes while the other thread
calls Client#receive).
I tried to place a Thread.pass between each iteration of the loop,
that's a little better but still far from what I expected.

Tested on Ruby 1.8.7 and ruby 1.9.1 (thought that native threads would
solve my problem, but not at all).

My code is the following :

require 'TestClientBase'
class Client < ClientBase

def initialize()
@thread = Thread.new(self) {
|client|
fin = false
while fin != true
msg = client.receive(100)
case msg
when TIME_OUT
puts ("Timeout")
when SERV_CLOSED
fin = true
end
Thread.pass
end
}
@thread.priority = -1
end
def wait()
@thread.join(10.0)
end
end


client = Client.new

100.times {
puts "Time=#{Time.now}"
}

client.wait()


Thanks for you replies.
 
K

KUBO Takehiro

How about rb_thread_polling() and WaitForSingleObject(handle, 0)?

while ((result =3D WaitForSingleObject(handle, 0)) =3D=3D WAIT_TIMEOUT) {
/* The current thread sleeps 0.06 second to make a time for other
threads to run. */
rb_thread_polling();
}
if (result =3D=3D WAIT_OBJECT_0) {
...
In 1.8, there's not really a good way to address this unless you can
convert the object you're waiting on to a file descriptor suitable to
wait on with rb_thread_select().

rb_thread_select() is best for file descriptors on Unix. But on Windows,
it works only for sockets. It assumes normal files are always readable/writ=
able.
See a comment in rb_w32_select() in win32/win32.c. Thus it is unusable in
this case.
In 1.9, there is additionally an API function --
rb_thread_blocking_region -- which you can use to wrap a function which
needs to block (provided that the function doesn't touch the Ruby
interpreter or Ruby objects in any way); rb_thread_blocking_region
permits other threads to continue while the function you pass to it
blocks.

Agree. It is better than rb_thread_polling() in 1.9.
 
K

KUBO Takehiro

How about rb_thread_polling() and WaitForSingleObject(handle, 0)?

Sorry, rb_thread_polling() returns immediately if only one thread runs.
rb_thread_wait_for() should be used instead.

=A0while ((result =3D WaitForSingleObject(handle, 0)) =3D=3D WAIT_TIMEOUT)=
{
struct timeval tv;
tv.sec =3D 0;
tv.usec =3D 100000;
=A0 =A0rb_thread_wait_for(rv); /* sleep 0.1 second */
=A0}
 
J

John Bob

Takehiro said:
Sorry, rb_thread_polling() returns immediately if only one thread runs.
rb_thread_wait_for() should be used instead.

while ((result = WaitForSingleObject(handle, 0)) == WAIT_TIMEOUT) {
struct timeval tv;
tv.sec = 0;
tv.usec = 100000;
rb_thread_wait_for(rv); /* sleep 0.1 second */
}

I used the rb_thread_blocking_region method, seems to work like a charm!
Thanks a lot for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top