I don't see any problem in declarations. If you are not sure in GCC atomics for some reason, use TBB atomics in which I'm sure.
But where I see the problem is usage of mutable
keyword in order to access fields from the const
functor. mutable
is often mark of a bad design. And in parallel programming its usage is especially error-prone. If a method which is called in parallel marked as const, it often means that it will be called in parallel on the same instance of the functor class. Thus you have a data-race on the mutable field.
Instead of breaking the const restrictions, you can move local_value into the scope of operator()
, or try thread-local containers tbb::combinable
or tbb::enumerable_thread_specific
to cache the thread-specific partial values, or apply the parallel reduction algorithm tbb::parallel_reduce
where you can safely store partial results in functor's body.