#include #include #include int main() { std::atomic< bool > kill{ false }; std::mutex mtx; int x = 0; std::thread t( [&] { while ( !kill ) { std::lock_guard< std::mutex > lock( mtx ); x = x % 2 + 1; } // unlock } ); { std::lock_guard< std::mutex > lock( mtx ); while ( x != 0 ) { } } // unlock kill = true; t.join(); }