Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread Safety

1 view
Skip to first unread message

jwesley

unread,
Nov 10, 2005, 12:37:31 PM11/10/05
to
I'm an experienced developer starting to learn Ruby. On the whole I
think Ruby is a great language. My concerns are with thread safety...

1) An exception is stored in a global variable, "$!". If multiple
threads have exceptions thrown, is the the thread guaranteed to get
it's own exception (not another threads) when evaluating the "$!". (I
know there is a work around by naming the exception.)

2) Does ruby commonly use global values to pass values...which is not
thread safe (unless it's somehow "thread-local").

3) Is there any plan to use OS-level threads in Ruby 2.0? A simple
thread example I wrote was incredibly slow and I think it was because
many "slow-to-return" OS calls were being made...

thanks,
Justin

Kirk Haines

unread,
Nov 10, 2005, 12:58:54 PM11/10/05
to
On Thursday 10 November 2005 10:42 am, jwesley wrote:

> 1) An exception is stored in a global variable, "$!". If multiple
> threads have exceptions thrown, is the the thread guaranteed to get
> it's own exception (not another threads) when evaluating the "$!". (I
> know there is a work around by naming the exception.)
>
> 2) Does ruby commonly use global values to pass values...which is not
> thread safe (unless it's somehow "thread-local").

Don't use globals. I can't think of a single instance where one needs to use
them, though maybe someone else can. So, if you do want to use them, use a
mutex.synchronize block around them.

> 3) Is there any plan to use OS-level threads in Ruby 2.0? A simple
> thread example I wrote was incredibly slow and I think it was because
> many "slow-to-return" OS calls were being made...

Yes. OS level threads are going to be supported in Ruby 2.0.

At the current time, if one has code that is making a lot of calls which will
block, the best alternative is to use a multiprocess model instead of a
multithreaded model.


Kirk Haines


Bill Kelly

unread,
Nov 10, 2005, 1:09:59 PM11/10/05
to
From: "Kirk Haines" <kha...@enigo.com>

>
> On Thursday 10 November 2005 10:42 am, jwesley wrote:
>
>> 1) An exception is stored in a global variable, "$!". If multiple
>> threads have exceptions thrown, is the the thread guaranteed to get
>> it's own exception (not another threads) when evaluating the "$!". (I
>> know there is a work around by naming the exception.)
>>
>> 2) Does ruby commonly use global values to pass values...which is not
>> thread safe (unless it's somehow "thread-local").
>
> Don't use globals. I can't think of a single instance where one needs to use
> them, though maybe someone else can. So, if you do want to use them, use a
> mutex.synchronize block around them.

But, yes, variables like $&, $`, $', $1..$n and I've no doubt $! are
really thread-local.


Regards,

Bill

jwesley

unread,
Nov 10, 2005, 1:30:29 PM11/10/05
to

jwesley

unread,
Nov 10, 2005, 1:34:21 PM11/10/05
to

Kirk Haines

unread,
Nov 10, 2005, 1:59:55 PM11/10/05
to
On Thursday 10 November 2005 11:09 am, Bill Kelly wrote:

> But, yes, variables like $&, $`, $', $1..$n and I've no doubt $! are
> really thread-local.

Which is cool, I agree, but in general, my position is still to not use
globals, and just sidestep the whole issue.


Kirk Haines


Mauricio Fernández

unread,
Nov 10, 2005, 7:23:15 PM11/10/05
to
On Fri, Nov 11, 2005 at 04:13:27AM +0900, Florian G. Pflug wrote:
> >But, yes, variables like $&, $`, $', $1..$n and I've no doubt $! are
> >really thread-local.
>
> $1..$n are even method-local, I believe.

all the vars derived from $~

--
Mauricio Fernandez


Robert Klemme

unread,
Nov 11, 2005, 3:48:06 AM11/11/05
to
0 new messages