www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - thread local singleton vs. static variables

reply "estew" <estewh gmail.com> writes:
Hi,

I've been looking at std.idoms:
http://someboddy.github.io/phobos/ddocs/for-idioms/idioms.html

I am wondering why I would need ThreadLocalSingleton when D has 
thread local static variables.

For example:
class A { // My thread local globals.
     private this() {}
     static int x;
     static this() {}
}
A.x = 10;

class B { // My thread local globals wrapped in a singleton.
    mixin ThreadLocalSingleton;
    private this() {}
    int x;
}
B.instance.x = 10;

What is the advantage of choosing B in preference to A?

Thanks,
Stewart
Jul 11 2013
parent "estew" <estewh gmail.com> writes:
Could it be something to do with purity, perhaps?
Jul 11 2013