[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby Extensions and Thread Safety

kapil.surlaker@gmail.com

1/20/2006 4:21:00 PM

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

4 Answers

Robert Klemme

1/20/2006 5:06:00 PM

0

kapil.surlaker@gmail.com wrote:
> 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

kapil.surlaker@gmail.com

1/20/2006 7:21:00 PM

0

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

Timothy Goddard

1/21/2006 10:52:00 AM

0

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.

Robert Klemme

1/23/2006 8:33:00 AM

0

Timothy Goddard wrote:
> 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