TLS variable for only one thread

IchorDev zxinsworld at gmail.com
Thu Feb 20 06:24:36 UTC 2025


On Saturday, 15 February 2025 at 10:09:39 UTC, Jonathan M Davis 
wrote:
> I think that you need to be clearer about what you're trying to 
> do. If a module-level variable is not shared, __gshared, or 
> immutable, then each thread gets a completely separate 
> variable, and no other thread has access to that object unless 
> you do something to pass it to another thread (which would 
> involve casting to shared or immutable and calling and then 
> using something like std.concurrency to pass it across), and 
> the only way to give another thread access to the variable 
> itself would be to take its address and cast the pointer to 
> shared or immutable to be able to pass it across threads. The 
> variable itself is restricted to a single thread, so it's 
> already the case that no other threads have access to a 
> non-shared variable.

Perhaps my use of the term TLS was unhelpful. I never studied 
multi-threading at the architectural level, so forgive me if my 
understanding is a bit fuzzy.
Let's say I have some important singleton class instance on 
thread A, but thread B doesn't need it, so it isn't `shared`:
```d
SingletonObject foo;
```
However, this means that thread B has its own version of `foo`, 
right? Which surely means:
1. Thread B is wasting space by storing a (null) pointer for its 
version of `foo` at all times; and
2. B could potentially start using its version of `foo`, which I 
really do not want.


More information about the Digitalmars-d-learn mailing list