C++同時実戦:面接問題1:マルチスレッドペン試験問題
769 ワード
テーマ:サブスレッドは10回循環して、それからメインスレッドは100回循環して、それからまたサブスレッドに戻って10回循環して、それからメインスレッドに戻ってまた100回循環して、このように50回循環して、コードを書き出してみます.
#include
#include
#include
#include
using namespace std;
mutex m;
condition_variable cond;
int flag=10;
void fun(int num){
for(int i=0;i<50;i++){
unique_lock lk(m);//A unique lock is an object that manages a mutex object with unique ownership in both states: locked and unlocked.
while(flag!=num)
cond.wait(lk);// wait lk.unlock()
for(int j=0;j