thread-safe shared field access

jj75607 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 30 07:10:54 PDT 2016


For example I have a shared from different threads variable. What 
piece of code is correctly thread-safe?

First:
   shared class A
   {
       shared(int) x;

       void test1()
       {
           x = 10;
           x += 5
           writeln(x);
       }
   }

Or second:
   import core.atomic;
   shared class A
   {
       shared(int) x;

       void test1()
       {
           atomicStore(x, 10);
           atomicOp!("+=")(x, 5);
           writeln(atomicLoad(x));
       }
   }


More information about the Digitalmars-d-learn mailing list