#define _GLIBCXX_USE_NANOSLEEP 1 #include #include #include #include void runthread(::std::mutex &m, ::std::condition_variable &v, bool &started) { ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); { ::std::unique_lock< ::std::mutex> lock(m); started = true; v.notify_one(); } ::std::cerr << "I am the new thread!\n"; } int main() { ::std::mutex m; ::std::condition_variable v; bool started = false; ::std::thread newthread(runthread, ::std::ref(m), ::std::ref(v), ::std::ref(started)); { ::std::unique_lock< ::std::mutex> lock(m); while (!started) { v.wait(lock); } } ::std::cerr << "Both I and the new thread are running.\n"; newthread.join(); return 0; }