#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
int count = 0;
const int total_count = 30;
void printLetter(const char letter, const int thread_id) {
for (int i = 0; i < total_count; ++i) {
std::unique_lock<std::mutex> lock(mtx);
while (thread_id != (count % 3)) {
cv.wait(lock);
}
std::cout << letter << std::endl;
count++;
cv.notify_all();
}
}
int main() {
std::thread t1(printLetter, 'A', 0);
std::thread t2(printLetter, 'B', 1);
std::thread t3(printLetter, 'C', 2);
t1.join();
t2.join();
t3.join();
return 0;
}
01-08
763
09-02
1238
09-09
676
09-08
978