static __gshared struct
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jul 1 16:36:35 PDT 2016
On Friday, 1 July 2016 at 23:26:19 UTC, Hiemlick Hiemlicker wrote:
> On Friday, 1 July 2016 at 23:03:17 UTC, Basile B. wrote:
>> On Friday, 1 July 2016 at 22:47:21 UTC, Hiemlick Hiemlicker
>> wrote:
> Ok, Does that mean
>
>
>> void main()
>> {
>> static struct Foo{}
> foo();
>> }
>
> void foo()
> {
> Foo f;
> }
>
> works?
No.
An example usage is for the singleton pattern:
T singletonViaFactory(T, A...)(A a)
if (is(T == class))
{
static T instance;
if (instance)
return instance;
else
return new T(a);
}
"instance" is well a global variable and not a local, but it's
hidden from the outside world.
> Also, then how do I declare a struct to be "global" so that it
> can be common to all threads? Do I have to declare _gshared for
> each element of the struct?
no, just put __gshared before the variable declaration, not for
each member:
struct Foo
{
int i;
}
__gshared Foo foo;
is fine.
More information about the Digitalmars-d-learn
mailing list