Thread overview | |||||
---|---|---|---|---|---|
|
August 27, 2017 Unable to set static data member of a class (results in default value 0) | ||||
---|---|---|---|---|
| ||||
I have: class DataSignal : Thread { public: static int dataReadDelay; void run() { while (true) { Thread.sleep(dur!"msecs"(dataReadDelay)); // Read in the new file data } } } in main I have: DataSignal.dataReadDelay = 8000; // initialize a bunch of signals Then when each thread is running they sleep for 0 seconds, and I can verify that dataReadDelay is staying at 0. I have initialized it no where else. This seems like a major bug. |
August 27, 2017 Re: Unable to set static data member of a class (results in default value 0) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Sunday, 27 August 2017 at 22:21:11 UTC, Enjoys Math wrote: > static int dataReadDelay; That's thread-local. Use shared to make it shared across all threads, and/or initialize it in the same thread as the use. See: https://dlang.org/migrate-to-shared.html https://tour.dlang.org/tour/en/multithreading/thread-local-storage |
August 27, 2017 Re: Unable to set static data member of a class (results in default value 0) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, August 27, 2017 22:29:46 Adam D. Ruppe via Digitalmars-d-learn wrote:
> On Sunday, 27 August 2017 at 22:21:11 UTC, Enjoys Math wrote:
> > static int dataReadDelay;
>
> That's thread-local. Use shared to make it shared across all threads, and/or initialize it in the same thread as the use.
>
>
> See:
> https://dlang.org/migrate-to-shared.html
> https://tour.dlang.org/tour/en/multithreading/thread-local-storage
Yeah, so the thread with main in it should have the value set, but for every other thread, it would be default-initialized. If you're just looking to set it that one value for all threads and not change it, I'd suggest that you just set it directly and make it immutable - or use enum, so it's a manifest constant. If you want to change it though, you will need to use shared and deal with protecting it appropriately when accessing it.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation