I have a thread that I want to sit in the loop until I can exit the program, and at what point I want it to exit the loop and exit, so I can call std::thread::join on it. In the days of C ++ 03, I would just use a lock protected bool to tell the thread when to exit. This time I thought that I would use the new Atomics library (especially std::atomic_bool ), but I have problems. Below is my test case:
#include <atomic> #include <thread> #include <cstdio> using namespace std; void setBool(atomic_bool& ab) { ab = true; } int main() { atomic_bool b; b = false; thread t(setBool, b); t.join(); printf("Atomic bool value: %d\n", b.load()); return 0; }
A thread t declaration splashes out this monster when trying to compile. The central part of the error is as follows:
incorrect initialization of a non-constant link of type 'std :: atomic_bool & from rvalue of type' std :: atomic_bool
Why can't I get a link to atomic_bool ? What should I do instead?
Matt kline
source share