3 ms·
https://en.wikipedia.org/wiki/Thread-local_storage https://en.wikipedia.org/wiki/Thread-local_storage mentions the two most common uses: errno (historically a g
by Someone 6y ago
https://en.wikipedia.org/wiki/Thread-local_storage https://en.wikipedia.org/wiki/Thread-local_storage mentions the two most common uses: errno (historically a global variable, but on modern Unixes thread-local) and having separate threads accumulate a value, to only merge them at the end, thus decreasing contention for the merged value.
- platz 6y ago> having separate threads accumulate a value, to only merge them at the end, thus decreasing contention for the merged value. That sounds like just having scoped state in each thread that isn't global.
- saagarjha 6y agoTLS lets you name that state with the same name across threads.
- Someone 6y agoYes, but sometimes, you can’t do that. For example, programming languages with a ‘parallel for’ may use a thread pool, reusing threads between iterations, but there won’t be scope to store the state in. So, what you do in the loop: - grab thread-local variable containing local accumulator - if it doesn’t exist yet, create it and add it to the global list of all local accumulators (there’s a race here, but it will be rarely run) - do the work for the iteration - update accumulator Then, after the loop, go through the global list of accumulators and merge them.