[dmd-concurrency] synchronized, shared, and regular methods inside the same class

Álvaro Castro-Castilla alvaro.castro.castilla at gmail.com
Tue Jan 5 15:31:50 PST 2010


2010/1/5, Sean Kelly <sean at invisibleduck.org>:
> On Jan 4, 2010, at 3:58 PM, Andrei Alexandrescu wrote:
>
>> Of course that doesn't avoid the race condition though. If the user would
>> have to call atomicIncrement(x) that would be clearly an improvement, but
>> even this would be an improvement:
>>
>> int y = sharedRead(x);
>> ++y;
>> sharedWrite(y, x);
>>
>> When writing such code the user inevitably hits on the documentation for
>> the two intrinsics, which clearly define their guarantees: only the
>> sequence of sharedRead and sharedWrite is preserved. At that point,
>> inspecting the code and understanding how it works is improved.

Is there a way that to make this eventually generalizable to any
operation or groups of operations that should be performed atomically?

something like:

atomic {
  ++x;
}

or even

atomic ++x;
atomic(++)x;

that would create a variable doing the work of "y", but in case you
want to specify the operations.

atomic {
  int y = x;
  y++;
  x = y;
}

"y++" won't be performed as an atomic operation because the compiler
will detect is a variable not shared. So it would be about the same
thing as the previous.

This could lead to something usable as a basic STM. Is it too hard to
implement? STM has already been discarded from D? I know also that you
want to avoid new reserved keywords (what about scope(atomic) :P).

Best Regards,

Álvaro Castro-Castilla


More information about the dmd-concurrency mailing list