I have a thread pool, each thread contains a counter (mostly TLS).
The main thread is required for frequent updates, calculating the sum of all local thread counters.
In most cases, each thread increments its own counter, so synchronization is not required.
But at a time when the main thread is being updated, I, of course, need some kind of synchronization.
I came up with MSVS ( _InterlockedXXX) built-in functions and it showed excellent performance (~ 0.8 s in my test) However, it limits my code to MSVC compilers and X86 / AMD64 platforms, but does it have a C ++ - portable way to do this?
I tried changing int type to std::atomic<int>for counter using std::memory_order_relaxedfor increment, but this solution is very slow! (~ 4s)
When using the base element std::atomic<T>::_My_val, access to it is non-atomic, as I would like, but it is not portable, so the problem is the same ...
Using the only one std::atomic<int>shared by all threads is even slower due to high competition (~ 10 s)
Do you have any ideas? Maybe I should use the (boost) library? Or write my own class?
source
share