2 ms·
Yes, 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 t
by Someone 6y ago
Yes, 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.