C++ 三个线程顺序打印ABC

#include
#include
#include
#include
using namespace std;
mutex mu;
std::condition_variable cond_var;
int num=0;
void func(char ch)
{
	int n=ch-'A';
	for(int i=0;i<10;i++)
	{
		std::unique_lock mylock(mu);
		cond_var.wait(mylock,[n]{return n==num;});
		cout< pool;
	pool.push_back(thread(func,'A'));
	pool.push_back(thread(func,'B'));
	pool.push_back(thread(func,'C'));
	for(auto iter=pool.begin();iter!=pool.end();iter++)
	{
		iter->join();
	}
	return 0;
}

输出:

你可能感兴趣的:(C++,笔试)