Question

Let's say I have the following declaration:

thread_local std::atomic<void*> local_var;

Are modifications of local_var from other threads allowed? That is, if I communicate the address of the local_var to another thread, won't modifying local_var from there summon the nasal demons?

Was it helpful?

Solution

Yes, it is allowed to modify another thread's thread-local variables.

What thread_local means for a variable is that the name refers to a different object in each thread, but it doesn't mean the object is private to its thread. If you make the variable known by something other than its name, i.e. its address, then other threads can access it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top