Ruby Extensions and Thread Safety

K

kapil.surlaker

I am pretty new to Ruby and I'm currently experiencing problems with C
extension modules.

I read some blurbs about ruby not being thread safe etc. Is there an
issue if an extension call results in spawning of multiple threads ?
Could this cause problems ?

Thanks for all your help.


Kapil
 
R

Robert Klemme

I am pretty new to Ruby and I'm currently experiencing problems with C
extension modules.

I read some blurbs about ruby not being thread safe etc. Is there an
issue if an extension call results in spawning of multiple threads ?
Could this cause problems ?

Yes.

The long story is: Ruby does not support native threads so it runs in a
single thread and expects no other thread to manipulate the data it is
working on. So *if* you create an extension that creates threads you
should do it in a way that the extension works on it's own set of data and
does proper synchronization. You'll then have to make sure the part of
the extension that is called from ruby code accesses the extension's data
thread safely. It could still be that you run into problems (for example
with malloc() et. al) - I'm not too deep into C multithreaded programming
and Ruby's internals with regard to memory mgmt.

I expect Ruby 2 to support native threads but I can't tell you when it
will be out.

HTH

robert
 
K

kapil.surlaker

Thanks for the response.

The library call (that spawns multiple threads) that the extension
makes in not written by me and there is no way I can control how it
creates and handles the threads it creates. If I can't call it from
ruby, what are my options ? I was thinking about creating a C program
that uses the library in question and that spawning it in another
process and grabbing its output in ruby.

Any other options ?

Thanks a lot for your help.
Kapil
 
T

Timothy Goddard

If the library is completely separated from ruby iself and its only
interface with the ruby interpreter is through a single thread, I don't
think you should have problems. You just don't want any part of the
ruby interpreter running in two threads of the same process at once. I
could be wrong, but I'd just give it a test. The worst you can do is
crash it.
 
R

Robert Klemme

Timothy said:
If the library is completely separated from ruby iself and its only
interface with the ruby interpreter is through a single thread, I
don't think you should have problems. You just don't want any part of
the ruby interpreter running in two threads of the same process at
once. I could be wrong, but I'd just give it a test. The worst you
can do is crash it.

+1 - Completely agree to all your points.

robert
 

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

Similar Threads


Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top