www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - unsynchronized access to primitive variables

reply luka8088 <luka8088 owave.net> writes:
Hello to all,

I would like to know if D guarantees that access to primitive variable 
is atomic ?

I was looking for any source of information that says anything about 
unsynchronized access to primitive variables. What I want to know is if 
it is possible (in any way and any OS / hardware) for the following code 
to output anything other then 1 or 2:

import std.stdio;
import core.thread;

void main () {
   int value = 1;
   (new Thread({ value = 2; })).start();
   writeln(value);
}

Thanks !
May 19 2012
next sibling parent reply "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Saturday, 19 May 2012 at 08:16:39 UTC, luka8088 wrote:
 Hello to all,

 I would like to know if D guarantees that access to primitive 
 variable is atomic ?

 I was looking for any source of information that says anything 
 about unsynchronized access to primitive variables. What I want 
 to know is if it is possible (in any way and any OS / hardware) 
 for the following code to output anything other then 1 or 2:

 import std.stdio;
 import core.thread;

 void main () {
   int value = 1;
   (new Thread({ value = 2; })).start();
   writeln(value);
 }

 Thanks !
This proggy will always print "1" because writeln() prints the value from the main thread. Spawned thread will have own value. Remember TLS is the default storage.
May 20 2012
parent "David Nadlinger" <see klickverbot.at> writes:
On Sunday, 20 May 2012 at 13:51:45 UTC, Dejan Lekic wrote:
 This proggy will always print "1" because writeln() prints the 
 value from the main thread. Spawned thread will have own value. 
 Remember TLS is the default storage.
This is wrong. _Global_ (or static) variables are in TLS by default, but local variables aren't automagically »thread local« in any sense – the fact that value is implicitly heap-allocated due to the closure does not change that. David
May 20 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 19 May 2012 04:16:39 -0400, luka8088 <luka8088 owave.net> wrote:

 Hello to all,

 I would like to know if D guarantees that access to primitive variable  
 is atomic ?

 I was looking for any source of information that says anything about  
 unsynchronized access to primitive variables. What I want to know is if  
 it is possible (in any way and any OS / hardware) for the following code  
 to output anything other then 1 or 2:

 import std.stdio;
 import core.thread;

 void main () {
    int value = 1;
    (new Thread({ value = 2; })).start();
    writeln(value);
 }

 Thanks !
It depends on hardware architecture. loads and stores of word sizes are generally atomic. A better (more portable) version would be to make value size_t or ptrdiff_t, which should mimic CPU word size. In very crude multithreading apps, I rely on this all the time. But you have to be careful not to change the value in more than one thread, or you are subject to racing. -Steve
May 23 2012