Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to call Ruby from non-interpreter native thread?

Thread Tools

How to call Ruby from non-interpreter native thread?

-= Ko =- Black Ninja
Posts: n/a
Hi everybody,

i start using Ruby interpreter and wrote some Ruby extension on C++ on
Win32 platform. I embedded Ruby extension with SWIG. So my extension
provide some communication capabilities, i.e. create server endpoint and
wait for messages. If in extension i make WaitForSingleObject() or
another blocking operation that will block interpreter thread, if i call
method from Ruby, which implement block operation. Besides, i run some
parallel Ruby threads, which are blocked too.

So, i can't receive messages synchronous and have to use asynchronous
mechanism. Thus, i create new native by CreateThread() (non-interpreter
native thread), when i call Run() method of my extension class. And no
blocks occur, control return to Ruby interpreter thread.

Below is a part of my Ruby code, that starts new thread, which starts
new server and listen for messages. RubyCallback is my extension class,
that implement callback interface from C++ into Ruby. RubyCallback class
has call() method, which call rb_funcall() function. If no messages
arrived then receiver_thread will suspend until new message arrives and
callback resume the thread, that start Receive() method.

receiver_thread = do
cb =, "wakeup")

ccinit =
ccinit.pAlertObject = cb #register callback

cc =
ret = cc.Init(ccinit)
if (ret)
puts "InterComm INIT OK"
ret = cc.Run() # here new native listener thread starts
if (ret)
puts "InterComm RUN OK"

loop do
mess = cc.Receive()
if (mess == nil)
puts "not Receive, fall a sleep...\n"
Thread.stop # thread self suspended if
# no new messages arrived
puts mess.MessageData


When new message arrived extension call callback function from Ruby to
say, that extension have new message. I implement C-Ruby callback
interface and call rb_funcall(...), but application starts to work very
strange: very seldom it works well just for one time, but often it may
crashed or deadlock in rb_funcall() call.

I know that Ruby interpreter isn't thread safe. Advice me how to
properly call Ruby function from C in interpreter thread context from
non-interpreter native thread? Or how can i schedule or queued call Ruby
function in properly way?

Thanx in advance!

Posted via

Reply With Quote
-= Ko =- Black Ninja
Posts: n/a
I've got skill with GIL (Global Interpreter Lock) and used
rb_thread_blocking_region() function. So, this thread can be close

thanx to myself =))

Posted via

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
jni native method indirect call of shmat returns invalid pointer Java 2 05-10-2007 09:21 PM
Java Native Interface: "Translate" java call to JNI Java 13 10-09-2006 04:52 PM
Call from attached thread in native code via JNI crashes. Why? Java 2 08-02-2006 07:06 AM
Need to call mixed-mode dll from native code. Mike Kamzyuk C++ 1 04-26-2005 01:35 PM