#include<thread>
#include<iostream>
#include<mutex>
#include<vector>
#include<condition_variable>
using namespace std;
mutex mtx;
condition_variable cond;
char s[] = { 'A','B','C' };
char message = 'A';
void handler(int i) {
for (int j = 0; j < 10; j++) {
unique_lock<mutex> lk(mtx);
while (message != s[i])
cond.wait(lk);
cout << s[i] << endl;
message = s[(i + 1) % 3];
lk.unlock();
cond.notify_all();
}
}
int main() {
thread thr[3];
for (int i = 0; i < 3; i++) {
thr[i] = thread(handler, i);
}
for (int i = 0; i < 3; i++) {
thr[i].join();
}
system("pause");
return 0;
}