I’ve been using volatile bool for years for thread execution control and it worked fine
// in my class declaration
volatile bool stop_;
-----------------
// In the thread function
while (!stop_)
{
do_things();
}
Now, since C++11 added support for atomic operations, I decided to try that instead
// in my class declaration
std::atomic<bool> stop_;
-----------------
// In the thread function
while (!stop_)
{
do_things();
}
But it’s several orders of magnitude slower than the volatile bool!
Simple test case I’ve written takes about 1 second to complete with volatile bool approach. With std::atomic<bool> however I’ve been waiting for about 10 minutes and gave up!
I tried to use memory_order_relaxed flag with load and store to the same effect.
My platform:
- Windows 7 64-bit
- MinGW gcc 4.6.x
What I’m doing wrong?
NB: I know that volatile does not make a variable thread-safe. My question is not about volatile, it’s about why atomic is ridiculously slow.
Code from “Olaf Dietsche”
IF YOU ARE USING GCC SMALLER 4.7
http://gcc.gnu.org/gcc-4.7/changes.html
So yeah .. only solution is to upgrade to GCC 4.7