In D, the preferred way to do multi-threading
is to rely on
immutable data and synchronize threads
using message passing. However, the language has built-in
support for synchronization primitives as well as
type system support with
shared to mark objects
that are accessed from multiple threads.
shared type identifier to mark variables
that are shared among different threads:
shared(int)* p = new int;
int* t = p; // ERROR
std.concurrency.send only allows sending either
shared data and copying the message
to be sent.
shared is transitive so if a
shared all its members will be too.
static variables aren't
default because they are implemented using
thread local storage (TLS) and each thread gets
its own variable.
synchronized blocks are used to tell the compiler
to create a critical section that can only be entered
by one thread at a time.
class member functions these blocks might be
limited to different member objects mutexes
synchronized(member1, member2) to reduce
contention. The D compiler inserts critical
sections automatically. A whole class can be marked
synchronized as well in which case the compiler will
make sure that just one thread accesses a concrete
instance of it at a time.
Atomic operations on
shared variables can be
performed using the
shared int test = 5;