I have tried to replace QMutex
in my application (monte carlo simulation) by std::mutex
, and surprisingly, the computation speed was divided by 3. The mutex locking/unlocking performance cost rised from negligible to about 66% of the threads time.
I digged into implementations sources. I initially thought that both were wrappers to Win32 threads (with an extra layer of pthread for std::thread
), but in fact Qt is not using any kernel functions for the mutexes, and has its own internal implementation based on atomic variables. This one seems much faster.
- Is it really possible to fully implement a mutex with only atomic variables? Does it have limitations?
- Why is this not used in the STL?
- What would be the guidelines for fully avoiding mutexes in my case? I am accumulating (additive operations) values over floating point buffers, shared between multiple threads
Thanks