4 ms·
> 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 scop
by 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.