# Is "a,b=c,d" atomic? or do I need a Mutex?

**URL:** <https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636>\
**Category:** ruby-talk\
**Created:** [10 June 2012 15:14 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636 "2012-06-10T15:14:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 June 2012 15:14 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/1 "2012-06-10T15:14:25Z")

</div>

Hi, I have two methods that could be called by different threads and  
both use the same array:

&nbsp;&nbsp;def method1 object  
&nbsp;&nbsp;&nbsp;&nbsp;MyModule.array \<\< object  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method2  
&nbsp;&nbsp;&nbsp;&nbsp;array, MyModule.array = MyModule.array, nil  
&nbsp;&nbsp;end

Do I need a Mutex (#synchronize() method) here? I think the answer is  
NOT since both actions in the array are "atomic". Am I right?

> **···**
>
> --  
> Iñaki Baz Castillo  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 June 2012 15:33 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/2 "2012-06-10T15:33:25Z")

</div>

Auto-reply: Yes, it's atomic:

&nbsp;&nbsp;def test a,b ; $a, $b = a, b ; end  
&nbsp;&nbsp;times = 10000000  
&nbsp;&nbsp;t1 = Thread.new { 1.upto(times) do |i| test(i,i) end }  
&nbsp;&nbsp;t2 = Thread.new { 1.upto(times) { raise "NOT EQUAL" if $a!=$b } }  
&nbsp;&nbsp;t1.join  
&nbsp;&nbsp;t2.join  
&nbsp;&nbsp;puts "OK"

=\> "OK"

> **···**
>
> 2012/6/10 Iñaki Baz Castillo \<ibc@aliax.net\>:
> 
> > Hi, I have two methods that could be called by different threads and  
> > both use the same array:
> > 
> > def method1 object  
> > MyModule.array \<\< object  
> > end
> > 
> > def method2  
> > array, MyModule.array = MyModule.array, nil  
> > end
> > 
> > Do I need a Mutex (#synchronize() method) here? I think the answer is  
> > NOT since both actions in the array are "atomic". Am I right?
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [10 June 2012 16:51 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/3 "2012-06-10T16:51:10Z")

</div>

It is atomic on MRI. It may or may not be on other interpreters, if  
you care about them. (But probably should be as an element of the  
syntax.)

Array#\<\< may or may not be atomic as well. (It is on MRI.)

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![Tony\_Arcieri3](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tony\_Arcieri3](https://rubytalk.org/u/Tony_Arcieri3)\
**Post date:** [10 June 2012 17:16 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/4 "2012-06-10T17:16:01Z")

</div>

Appending to an array from many threads in an unsynchronized manner can  
definitely raise a ConcurrentModificationException on JRuby

> **···**
>
> On Sun, Jun 10, 2012 at 9:51 AM, Bartosz Dziewoński \<matma.rex@gmail.com\>wrote:
> 
> > It is atomic on MRI. It may or may not be on other interpreters, if  
> > you care about them. (But probably should be as an element of the  
> > syntax.)
> > 
> > Array#\<\< may or may not be atomic as well. (It is on MRI.)
> > 
> > -- Matma Rex
> 
> --  
> Tony Arcieri

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [10 June 2012 17:52 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/5 "2012-06-10T17:52:20Z")

</div>

Thanks, so I've no problem since I'm coding for MRI.

> **···**
>
> 2012/6/10 Bartosz Dziewoński \<matma.rex@gmail.com\>:
> 
> > It is atomic on MRI. It may or may not be on other interpreters, if  
> > you care about them. (But probably should be as an element of the  
> > syntax.)
> > 
> > Array#\<\< may or may not be atomic as well. (It is on MRI.)
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/charles_nutter/32/2567_2.png) [@Charles\_Nutter](https://rubytalk.org/u/Charles_Nutter)\
**Post date:** [26 June 2012 01:23 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/6 "2012-06-26T01:23:46Z")

</div>

I would not bet on it being atomic, since Ruby's context-switch  
boundaries are not formally defined. However, atomicity is only  
important if you expect that a non-atomic assignment would produce  
different results than an atomic one, which is unlikely in this case.

Of course, if it matters, you shouldn't rely on this being atomic.

- Charlie

> **···**
>
> On Sun, Jun 10, 2012 at 11:51 AM, Bartosz Dziewoński \<matma.rex@gmail.com\> wrote:
> 
> > It is atomic on MRI. It may or may not be on other interpreters, if  
> > you care about them. (But probably should be as an element of the  
> > syntax.)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 June 2012 07:52 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/7 "2012-06-11T07:52:47Z")

</div>

Sorry, but I think this is the wrong attitude. Even MRI's  
implementation can change and the default should be to properly  
synchronize access to shared resources - if only for documenting the  
shared access. Note also that multiple threads may invoke #method2  
which makes synchronization necessary even in absence of swapping.

Kind regards

robert

> **···**
>
> On Sun, Jun 10, 2012 at 7:52 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > 2012/6/10 Bartosz Dziewoński \<matma.rex@gmail.com\>:
> > 
> > > It is atomic on MRI. It may or may not be on other interpreters, if  
> > > you care about them. (But probably should be as an element of the  
> > > syntax.)
> > > 
> > > Array#\<\< may or may not be atomic as well. (It is on MRI.)
> > 
> > Thanks, so I've no problem since I'm coding for MRI.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [11 June 2012 08:00 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/8 "2012-06-11T08:00:11Z")

</div>

In my case #method2 is private and is just called by the library and  
always in the same thread (the thread in which a C event loop runs).

I do agree that, while in Ruby land, I should use a mutex (not  
required right now since Array#\<\< and a,b=c,d are atomic, but this  
could change). However I'm re-writing my #method1 and #method2 in C  
so, for sure I don't need mutex. Please correct me if I'm wrong.

Thanks a lot.

> **···**
>
> 2012/6/11 Robert Klemme \<shortcutter@googlemail.com\>:
> 
> > Sorry, but I think this is the wrong attitude. Even MRI's  
> > implementation can change and the default should be to properly  
> > synchronize access to shared resources - if only for documenting the  
> > shared access. Note also that multiple threads may invoke #method2  
> > which makes synchronization necessary even in absence of swapping.
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Tony\_Arcieri3](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tony\_Arcieri3](https://rubytalk.org/u/Tony_Arcieri3)\
**Post date:** [11 June 2012 16:00 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/9 "2012-06-11T16:00:56Z")

</div>

Note that Array#\<\< isn't atomic, it's just that the GIL prevents contention  
between threads so it never becomes a problem (until you try to use a Ruby  
implementation without a GIL)

> **···**
>
> On Mon, Jun 11, 2012 at 1:00 AM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > I do agree that, while in Ruby land, I should use a mutex (not  
> > required right now since Array#\<\< and a,b=c,d are atomic
> 
> --  
> Tony Arcieri

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [11 June 2012 16:05 UTC](https://rubytalk.org/t/is-a-b-c-d-atomic-or-do-i-need-a-mutex/65636/10 "2012-06-11T16:05:12Z")

</div>

Well, since I'm coding a C extension for Ruby 1.9 MRI, I assume I can  
rely on the GVL 🙂

> **···**
>
> 2012/6/11 Tony Arcieri \<tony.arcieri@gmail.com\>:
> 
> > On Mon, Jun 11, 2012 at 1:00 AM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> > 
> > > I do agree that, while in Ruby land, I should use a mutex (not  
> > > required right now since Array#\<\< and a,b=c,d are atomic
> > 
> > Note that Array#\<\< isn't atomic, it's just that the GIL prevents contention  
> > between threads so it never becomes a problem (until you try to use a Ruby  
> > implementation without a GIL)
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>
