digitalmars.D.learn - static __gshared struct
- Hiemlick Hiemlicker (1/1) Jul 01 2016 what exactly does this do? are all members _gshared?
- Basile B. (15/16) Jul 01 2016 In this case __gshared is a complete NOOP. __gshared has only an
- Hiemlick Hiemlicker (11/33) Jul 01 2016 foo();
- Basile B. (22/39) Jul 01 2016 No.
- Hiemlick Hiemlicker (10/54) Jul 01 2016 I use a struct with static members so I do not have to
- Nicholas Wilson (15/75) Jul 01 2016 __gshared is a storage modifier only NOT a type modifier. As
- Mike Parker (14/23) Jul 01 2016 struct Foo {
- Hiemlick Hiemlicker (2/30) Jul 02 2016 Thanks, that will work.
what exactly does this do? are all members _gshared?
Jul 01 2016
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
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 meanwhat 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.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
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: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.On Friday, 1 July 2016 at 22:47:21 UTC, Hiemlick Hiemlicker wrote:Ok, Does that meanvoid 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?no, just put __gshared before the variable declaration, not for each member: struct Foo { int i; } __gshared Foo foo; is fine.
Jul 01 2016
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: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.On Friday, 1 July 2016 at 23:03:17 UTC, Basile B. wrote: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.On Friday, 1 July 2016 at 22:47:21 UTC, Hiemlick Hiemlicker wrote:Ok, Does that meanvoid 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?no, just put __gshared before the variable declaration, not for each member: struct Foo { int i; } __gshared Foo foo; is fine.
Jul 01 2016
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:__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; }On Friday, 1 July 2016 at 23:26:19 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.On Friday, 1 July 2016 at 23:03:17 UTC, Basile B. wrote: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.[...]Ok, Does that mean[...]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?no, just put __gshared before the variable declaration, not for each member: struct Foo { int i; } __gshared Foo foo; is fine.
Jul 01 2016
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
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:Thanks, that will work.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 02 2016