# Newby question: Is += atomic for integers?

**URL:** <https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344>\
**Category:** Ruby\
**Created:** [March 1, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344 "2006-03-01T23:09:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rj-cole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rj-cole](https://www.ruby-forum.com/u/rj-cole)\
**Post date:** [March 1, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344/1 "2006-03-01T23:09:15Z")

</div>

Hi,

Can the following program produce counts that are wrong?

class Test  
attr\_accessor :count  
def initialize  
@count = 0  
end  
end

$test = Test.new

def my\_function\_called\_by\_many\_threads  
…  
$test.count += 1  
…  
end

Any why is the following a syntax error?

class Test  
def +=(value)  
end  
end

i.e why can’t I define the += operator?

regards,

Richard.

---

<div class="post-metadata">

**Author:** ![rj-cole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rj-cole](https://www.ruby-forum.com/u/rj-cole)\
**Post date:** [March 2, 2006, 7:18am UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344/2 "2006-03-02T07:18:49Z")

</div>

rj-cole wrote:

> def my\_function\_called\_by\_many\_threads  
> …  
> $test.count += 1  
> …  
> end

Yes.

There’s two problem.

(1) Even though an operation seems to be atomic, say, “a+1”, it might  
not be atomic in the machine level. Such an example is operations on  
double, and class references. In some cases, assignment of double  
variable might be done two steps.

(2) Especially if $test were shared by multiple threads which runs on  
SMP, though current version of ruby core does not support it - I hope  
it will - , you have to use mutex(synchronize). Otherwise, different  
threads may see different values because each processor uses own cache.  
This rule even applies to Java, C++, etc.

Simple and best solution to these problems is to use synchronization  
whenever needed.

Minkoo S.

---

<div class="post-metadata">

**Author:** ![rj-cole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rj-cole](https://www.ruby-forum.com/u/rj-cole)\
**Post date:** [March 1, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344/3 "2006-03-01T23:11:34Z")

</div>

On Mar 1, 2006, at 2:08 PM, rj-cole wrote:

> $test = Test.new
> 
> def my\_function\_called\_by\_many\_threads  
> …  
> $test.count += 1  
> …  
> end

Yes. += is just syntax sugar. It gets expanded:

$ parse\_tree\_show -f  
a += 1  
[[:class,  
:Example,  
:Object,  
[:defn,  
:example,  
[:scope,  
[:block,  
[:args],  
[:lasgn, :a, [:call, [:lvar, :a], :+, [:array, [:lit, 1]]]]]]]]]

> Any why is the following a syntax error?
> 
> class Test  
> def +=(value)  
> end  
> end
> 
> i.e why can’t I define the += operator?

+= is not an operator, it is syntax sugar.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![rj-cole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rj-cole](https://www.ruby-forum.com/u/rj-cole)\
**Post date:** [March 2, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344/4 "2006-03-02T21:01:37Z")

</div>

Quoting Minkoo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Simple and best solution to these problems is to use  
> synchronization whenever needed.

And following from that, use as little shared state as possible  
between threads, so synchronization is needed as little as  
possible.

The more synchronization you use, the harder it is to get right.

-mental

---

<div class="post-metadata">

**Author:** ![rj-cole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rj-cole](https://www.ruby-forum.com/u/rj-cole)\
**Post date:** [March 2, 2006, 11:43am UTC](https://www.ruby-forum.com/t/newby-question-is-atomic-for-integers/54344/5 "2006-03-02T11:43:54Z")

</div>

Minkoo S. wrote:

> > $test = Test.new
> 
> Simple and best solution to these problems is to use synchronization  
> whenever needed.

I strongly support that! Always do proper synchronization even if a non  
synchronized version seems to work. It’s likely to break as soon as the  
code runs on another platform (e.g. Java VM). These bugs are hard to  
find  
and also there is a documentation advantage that comes with proper MT  
design.

Kind regards

```
robert
```
