写了个条件变量的小程序。感觉还是挺方便的。123456789101112131415161718192021222324252627282930313233using namespace std;mutex m_mtxCv;condition_variable m_Cv;void print(){ while(true) { unique_lock<mutex> lk(m_mtxCv); cout << " 1 " << endl; m_Cv.wait_for(lk,chrono::milliseconds(5000)); cout << " 2 " << endl; }}int main(){ thread t(print); while(true) { cin.get(); m_Cv.notify_one(); } return 0;}
编译
c++ test.cc -std=c++11 -lpthread