Forum: Ruby Newby question: Is += atomic for integers?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
92a2a8117d3bc6d32ad7dbf6776b10bf?d=identicon&s=25 rj-cole (Guest)
on 2006-03-01 23:09
(Received via mailing list)
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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-03-01 23:11
(Received via mailing list)
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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
47b36de21d7ecbc824c81d24802a6290?d=identicon&s=25 Minkoo Seo (pool007)
on 2006-03-02 07:18
(Received via mailing list)
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 Seo
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-02 11:43
(Received via mailing list)
Minkoo Seo 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
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-03-02 21:01
(Received via mailing list)
Quoting Minkoo Seo <minkoo.seo@gmail.com>:

> 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
This topic is locked and can not be replied to.