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

Andrei Alexandrescu andrei at erdani.com
Tue Jan 5 10:18:24 PST 2010


It is indeed broken, and the brokenness will be revealed by the 
following bits in the documentation:

"The language guarantees that the sequence of calls to sharedRead and 
sharedWrite is the same as syntactic order in each thread."

With this at hand, clearly there could be several possible interleavings 
of the calls from different thread, and the final value of x depends on 
the interleaving.


Andrei

Kevin Bealer wrote:
> On Mon, Jan 4, 2010 at 6:58 PM, Andrei Alexandrescu <andrei at erdani.com 
> <mailto:andrei at erdani.com>> wrote:
> 
> 
>     shared int x;
>     ...
>     ++x;
> 
>     The putative user notices that that doesn't work, so she's like,
>     meh, I'll do this then:
> 
>     int y = x;
>     ++y;
>     x = y;
> 
>     And the user remains with this impression that the D compiler is a
>     bit dumb. 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.
> 
>     ...
> 
>     Andrei
> 
>  
> Just to clarify, this is still broken, right?  I mean that if two users 
> are calling a method that does this,
> the value of x will only get incremented once if their calls to 
> sharedRead/sharedWrite are interleaved?
>  
> Kevin
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> dmd-concurrency mailing list
> dmd-concurrency at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-concurrency


More information about the dmd-concurrency mailing list