valid uses of shared

Artur Skawina art.08.09 at gmail.com
Sat Jun 9 04:18:36 PDT 2012


On 06/09/12 04:01, mta`chrono wrote:
>      private static shared int counter; // shared across all instances
>           auto i = ++counter;

What would you expect to happen here? Every thread to receive an unique
value, at least until the counter wraps around? Then evaluating '++counter'
needs to be atomic. How would that be implemented? Should the compiler do
this automatically? Would this be expected behavior from an increment
operator?

It gets even worse in the postincrement case - the naive rewriting that
then happens means it is practically impossible to implement it correctly
in a user defined type. So anything that relies on the result of 'counter++'
will be buggy - and you can't prevent this bug from happening.

   shared struct S { int x; @disable this(this); }
   shared S s;

   Error: cannot implicitly convert expression (this) of type shared(S) to S

Which may be related to the "bug" Steven filed, but fixing this by allowing
the implicit conversion would just hide the real problem.

artur


More information about the Digitalmars-d mailing list