实现双线程交替打印

C++实现双线程交替打印1~20之内的数。

std::mutex mtx;
std::condition_variable cond1, cond2;
static int num = 1;

void thread1(){
    while(true){
        unique_lock<mutex> lock(mtx);
        cout << "Thread 1 :" << num << endl;
        num++;
        cond2.notify_one();
        if(num >= 20)  // 线程退出时机,要唤醒另一个线程
            return;
        cond1.wait(lock);
        Sleep(1000);
    }
}
void thread2(){
    while(true){
        unique_lock<mutex> lock(mtx);
        cout << "Thread 2 :" << num << endl;
        num++;
        cond1.notify_one();
        if(num >= 20)
            return;
        cond2.wait(lock);
        Sleep(1000);
    }
}
void testAlternatePrinter(){
    thread t1(thread1);
    thread t2(thread2);
    t1.join();
    t2.join();
}

最后更新于