Thread overview
How does D lang handle multithreaded applications and static members of classes?
1 day ago
Inkrementator
1 day ago

Say I have a static context stack in my class Context, will each new thread receive their own copy of the static member or do I have to account for multiple threads and do it myself?

1 day ago

On Tuesday, 1 April 2025 at 23:01:23 UTC, Daniel Donnelly, Jr. wrote:

>

Say I have a static context stack in my class Context, will each new thread receive their own copy of the static member or do I have to account for multiple threads and do it myself?

All global variables in D, including static class members, are thread local by default. You have to explicitly add the __gshared attribute to the static variable to make it truly global.

1 day ago

On Wednesday, 2 April 2025 at 08:33:05 UTC, Inkrementator wrote:

>

On Tuesday, 1 April 2025 at 23:01:23 UTC, Daniel Donnelly, Jr. wrote:

>

Say I have a static context stack in my class Context, will each new thread receive their own copy of the static member or do I have to account for multiple threads and do it myself?

All global variables in D, including static class members, are thread local by default. You have to explicitly add the __gshared attribute to the static variable to make it truly global.

Thats a good thing. The default is great for my app.