std::mutex mut;
int iCnt = 0;
void DoA()
{
while (1)
{
mut.lock();
if (iCnt % 3 == 0)
{
printf("A\n");
iCnt++;
}
mut.unlock();
}
}
void DoB()
{
while (1) {
mut.lock();
if (iCnt % 3 == 1)
{
printf("B\n");
iCnt++;
}
else
{
}
mut.unlock();
}
}
void DoC()
{
while (1)
{
mut.lock();
if (iCnt % 3 == 2)
{
printf("C\n");
iCnt++;
}
mut.unlock();
}
}
int main2()
{
thread* t1 = new thread(DoA);
thread* t2 = new thread(DoB);
thread* t3 = new thread(DoC);
t1->join();
t2->join();
t3->join();
return 0;
}
C++ 顺序循环打出ABC
最新推荐文章于 2024-04-29 11:56:38 发布