www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static __gshared struct

reply Hiemlick Hiemlicker <HH reign.com> writes:
what exactly does this do? are all members _gshared?
Jul 01 2016
parent reply Basile B. <b2.temp gmx.com> writes:
On Friday, 1 July 2016 at 22:47:21 UTC, Hiemlick Hiemlicker wrote:
 what exactly does this do? are all members _gshared?
In this case __gshared is a complete NOOP. __gshared has only an effect on variables. It prevents them to reside in the TLS, so that they can be used by any thread of the program (even if then critical sections or atomic read/write are then necessary). Static means that the declaration is like if in the global scope: void main() { static struct Foo{} } is like struct Foo{} void main(string[] args) {} It's not considered nested anymore.
Jul 01 2016
parent reply Hiemlick Hiemlicker <HH reign.com> writes:
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:
 what exactly does this do? are all members _gshared?
In this case __gshared is a complete NOOP. __gshared has only an effect on variables. It prevents them to reside in the TLS, so that they can be used by any thread of the program (even if then critical sections or atomic read/write are then necessary). Static means that the declaration is like if in the global scope: void main() { static struct Foo{} } is like struct Foo{} void main(string[] args) {} It's not considered nested anymore.
Ok, Does that mean
 void main()
 {
     static struct Foo{}
foo();
 }
void foo() { Foo f; } works? 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?
Jul 01 2016
parent reply Basile B. <b2.temp gmx.com> writes:
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.
Jul 01 2016
parent reply Hiemlick Hiemlicker <HH reign.com> writes:
On Friday, 1 July 2016 at 23:36:35 UTC, Basile B. wrote:
 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.
I use a struct with static members so I do not have to instantiate it. It is essentially a singleton. I want all the variables to be __gshared. I guess I have to prefix all variables with it? Basically I have Foo.i; on foo. i is static, of course. I also want it to be __gshared. Makes sense to me that __gshared struct x; all of x's variables should be __gshared.
Jul 01 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 2 July 2016 at 00:08:10 UTC, Hiemlick Hiemlicker 
wrote:
 On Friday, 1 July 2016 at 23:36:35 UTC, Basile B. wrote:
 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:
 [...]
Ok, Does that mean
     [...]
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.
I use a struct with static members so I do not have to instantiate it. It is essentially a singleton. I want all the variables to be __gshared. I guess I have to prefix all variables with it? Basically I have Foo.i; on foo. i is static, of course. I also want it to be __gshared. Makes sense to me that __gshared struct x; all of x's variables should be __gshared.
__gshared is a storage modifier only NOT a type modifier. As opposed to immutable which is both, i.e. one can mark a class immutable and then only create immutable classes. if you want to be able to use members without a declaration. then you must use struct Foo { static int i; } void main() { Foo.i = 42; }
Jul 01 2016
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 2 July 2016 at 00:08:10 UTC, Hiemlick Hiemlicker 
wrote:

 I use a struct with static members so I do not have to 
 instantiate it. It is essentially a singleton. I want all the 
 variables to be __gshared. I guess I have to prefix all 
 variables with it?

 Basically I have Foo.i; on foo. i is static, of course. I also 
 want it to be __gshared.

 Makes sense to me that

 __gshared struct x;

 all of x's variables should be __gshared.
struct Foo { __gshared: static int x; static float y; } Or: struct Foo { __gshared { static int x; static float y; } }
Jul 01 2016
parent Hiemlick Hiemlicker <HH reign.com> writes:
On Saturday, 2 July 2016 at 03:54:26 UTC, Mike Parker wrote:
 On Saturday, 2 July 2016 at 00:08:10 UTC, Hiemlick Hiemlicker 
 wrote:

 I use a struct with static members so I do not have to 
 instantiate it. It is essentially a singleton. I want all the 
 variables to be __gshared. I guess I have to prefix all 
 variables with it?

 Basically I have Foo.i; on foo. i is static, of course. I also 
 want it to be __gshared.

 Makes sense to me that

 __gshared struct x;

 all of x's variables should be __gshared.
struct Foo { __gshared: static int x; static float y; } Or: struct Foo { __gshared { static int x; static float y; } }
Thanks, that will work.
Jul 02 2016