# Is $! thread safe?

**URL:** <https://www.ruby-forum.com/t/is-thread-safe/107549>\
**Category:** Ruby\
**Created:** [June 23, 2007, 12:27am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549 "2007-06-23T00:27:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 12:27am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/1 "2007-06-23T00:27:48Z")

</div>

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

Is it?

Thanks.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

* * *

# 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

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 12:34am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/2 "2007-06-23T00:34:49Z")

</div>

On Sat, 23 Jun 2007 07:26:19 +0900, Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

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

-mental

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 12:36am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/3 "2007-06-23T00:36:49Z")

</div>

Erik V. 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]

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 12:50am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/4 "2007-06-23T00:50:49Z")

</div>

Gregory B. wrote:

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

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 12:37am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/5 "2007-06-23T00:37:49Z")

</div>

On 6/22/07, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sat, 23 Jun 2007 07:26:19 +0900, Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 4:25am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/6 "2007-06-23T04:25:06Z")

</div>

On 6/22/07, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gregory B. wrote:
> 
> > On 6/22/07, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 10:06am UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/7 "2007-06-23T10:06:26Z")

</div>

> > 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.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [June 23, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/is-thread-safe/107549/8 "2007-06-23T20:02:24Z")

</div>

Erik V. 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…
