[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is $! thread safe?

Erik Veenstra

6/22/2007 10:26:00 PM

>From the code below, it appears that $! is thread safe.

Is it?

Thanks.

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

# OUTPUT:
# T=1 -> [1, :a, 1]
# T=2 -> [2, :a, 2]
# T=3 -> [3, :b, 2]
# T=4 -> [4, :b, 1]

Thread.abort_on_exception = true

dt = lambda{t0=Time.now ; lambda{|*a| p [(Time.now-t0).to_i,
*a]}}[]

class MyException < StandardError
def initialize(n)
@n = n
end

def inspect
@n.to_s
end
end

(1..2).each do |m|
Thread.new(m) do |n|
begin
sleep(n) # t1 -> sleep(1) ; t2 -> sleep(2)

raise MyException.new(n)
rescue
dt[:a, $!] # t1 -> T=1 ; t2 -> T=2

sleep(5-2*n) # t1 -> sleep(3) ; t2 -> sleep(1)

dt[:b, $!] # t1 -> T=4 ; t2 -> T=3
end
end
end

sleep 5

----------------------------------------------------------------


7 Answers

MenTaLguY

6/22/2007 10:34:00 PM

0

On Sat, 23 Jun 2007 07:26:19 +0900, Erik Veenstra <erikveen@gmail.com> wrote:
> From the code below, it appears that $! is thread safe.
>
> Is it?

Each thread gets its own private $!, so yes.

-mental


Joel VanderWerf

6/22/2007 10:36:00 PM

0

Erik Veenstra wrote:
>>From the code below, it appears that $! is thread safe.
>
> Is it?

Yes, according to Pickaxe v2, p.319 (the "[thread]" is what signifies this):

Exception Information
$! The exception object passed to raise. [thread]
Exception
$@ Array The stack backtrace generated by the last exception.
See Kernel#caller
on page 497 for details. [thread]


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Gregory Brown

6/22/2007 10:37:00 PM

0

On 6/22/07, MenTaLguY <mental@rydia.net> wrote:
> On Sat, 23 Jun 2007 07:26:19 +0900, Erik Veenstra <erikveen@gmail.com> wrote:
> > From the code below, it appears that $! is thread safe.
> >
> > Is it?
>
> Each thread gets its own private $!, so yes.

Is that a special case for built in globals? I'm assuming so, but just curious.

Joel VanderWerf

6/22/2007 10:49:00 PM

0

Gregory Brown wrote:
> On 6/22/07, MenTaLguY <mental@rydia.net> wrote:
>> On Sat, 23 Jun 2007 07:26:19 +0900, Erik Veenstra <erikveen@gmail.com>
>> wrote:
>> > From the code below, it appears that $! is thread safe.
>> >
>> > Is it?
>>
>> Each thread gets its own private $!, so yes.
>
> Is that a special case for built in globals? I'm assuming so, but just
> curious.

No, only for a subset of them, including $SAFE and the pattern matching
vars.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Gregory Brown

6/23/2007 2:24:00 AM

0

On 6/22/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Gregory Brown wrote:
> > On 6/22/07, MenTaLguY <mental@rydia.net> wrote:

> >> Each thread gets its own private $!, so yes.
> >
> > Is that a special case for built in globals? I'm assuming so, but just
> > curious.
>
> No, only for a subset of them, including $SAFE and the pattern matching
> vars.

Ah, the pattern matching vars were actually the ones I was interested
in... Thanks.

Erik Veenstra

6/23/2007 8:05:00 AM

0

> > From the code below, it appears that $! is thread safe.
> >
> > Is it?
>
> Yes, according to Pickaxe v2, p.319 (the "[thread]" is what
> signifies this):
>
> Exception Information
> $! Exception The exception object passed to raise. [thread]

Page 319 of Pickaxe v2 doesn't tell me anything about
exceptions or global variables. But I found the same text on
page 334. (It's not easy to search in a printed book... :})

Page 108 of the same book (at least of _my_ instance) says
"Ruby places a reference to the associated Exception object
into the global variables $!."

I suppose "global" in this sentence means "context of current
thread" instead of "context of current program". It was a bit
confusing to me...

Thanks!

gegroet,
Erik V. - http://www.erikve...


Joel VanderWerf

6/23/2007 5:32:00 PM

0

Erik Veenstra wrote:
> Page 319 of Pickaxe v2 doesn't tell me anything about
> exceptions or global variables. But I found the same text on
> page 334. (It's not easy to search in a printed book... :})

Sorry about that. I didn't expect that the PDF and the printed copy
would have different pagination...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407