# Thread safety

**URL:** <https://www.ruby-forum.com/t/thread-safety/129251>\
**Category:** Ruby\
**Created:** [February 15, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/thread-safety/129251 "2008-02-15T19:54:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 15, 2008, 7:54pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/1 "2008-02-15T19:54:31Z")

</div>

With the discussion surrounding merb/rails and thread safety, I’ve been  
somewhat concerned with making my own tiny home-rolled web apps that I  
write thread safe, if only to become more knowledgeable on the subject.

I know it’s probably hard to generalize, but does anybody have any  
simple rules to follow (or resources to share) for writing thread safe  
code?

Thanks!

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 15, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/2 "2008-02-15T21:09:03Z")

</div>

On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:

> Thanks!  
> Anyone else jump right in here. These are mine:

Rule #1: Don’t use global variables.  
Rule #2: If you do, when you change them, wrap them in a blocking  
mechanism such as a mutex or critical section so two threads can’t try  
at once.  
Rule #3: If you are changing the state of an object that is not thread-  
local, wrap the change in a mutex (etc.)

So, for example:

require ‘thread’  
my\_object\_sync = Mutex.new

Thread.new do

# just lock on this object

my\_object\_sync.synchronize do  
# Update that is not atomic  
end  
end

Rule #4: Don’t be too sure that any operation in Ruby is atomic.

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 15, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/3 "2008-02-15T23:21:49Z")

</div>

On 15.02.2008 21:08, s.ross wrote:

> On Feb 15, 2008, at 10:54 AM, Jeff Turc wrote:
> 
> > I know it’s probably hard to generalize, but does anybody have any  
> > simple rules to follow (or resources to share) for writing thread safe  
> > code?

> Rule #3: If you are changing the state of an object that is not thread-  
> local, wrap the change in a mutex (etc.)

Corollary: use thread confinement, i.e. design your app in a way that  
most objects are used in a single thread at a time only. Restrict need  
for synchronization to the minimum needed.

> Rule #4: Don’t be too sure that any operation in Ruby is atomic.

Somehow this reminds me of “Blondie” and “Atomic”. 🙂 Those were the  
days…

Cheers

robert

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 15, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/4 "2008-02-15T23:25:57Z")

</div>

> Somehow this reminds me of “Blondie” and “Atomic”. 🙂 Those were  
> the days…

One way or another 🙂

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 15, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/5 "2008-02-15T23:31:18Z")

</div>

Steve R. wrote:

> > Somehow this reminds me of “Blondie” and “Atomic”. 🙂 Those were  
> > the days…
> 
> One way or another 🙂

I’m gonna getcha!

Thanks guys. big help.

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 17, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/thread-safety/129251/6 "2008-02-17T22:55:12Z")

</div>

For anyone else starting out with threads, I also stumbled across this:

8 Simple Rules for Designing Threaded Applications  
[http://www.devx.com/go-parallel/Article/37034](http://www.devx.com/go-parallel/Article/37034)

---

<div class="post-metadata">

**Author:** ![reversejt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@reversejt](https://www.ruby-forum.com/u/reversejt)\
**Post date:** [February 21, 2008, 7:14am UTC](https://www.ruby-forum.com/t/thread-safety/129251/7 "2008-02-21T07:14:09Z")

</div>

On Mon, 2008-02-18 at 06:55 +0900, Jeff Turc wrote:

> For anyone else starting out with threads, I also stumbled across this:
> 
> 8 Simple Rules for Designing Threaded Applications  
> [http://www.devx.com/go-parallel/Article/37034](http://www.devx.com/go-parallel/Article/37034)

I think this is a good article and I wouldn’t mind recommending it to  
others.

Thanks Jeff!

-mental
