注:
和同事讨论一个多线程的问题,想到前几天看到的一个关于多线程编程的题目。因为比较简单,利用中午吃完饭的间隙,随手实现了一下,在这里分享给大家。
/*
题目:
output "ABCABCABC": threadFunc_A just print "A", threadFunc_B just print "B", threadFunc_C just print "C".
*/
#include <pthread.h>
#include <iostream>
using namespace std;
bool is_A_ok = true;
bool is_B_ok = false;
bool is_C_ok = false;
static void* threadFunc_A(void* param)
{
int i = 3;
while(i > 0)
{
if(is_A_ok)
{
cout<<"A";
--i;
is_A_ok = false;
is_B_ok = true;
}
}
return NULL;
}
static void* threadFunc_B(void* param)
{
int i = 3;
while(i > 0)
{
if(is_B_ok)
{
cout<<"B";
--i;
is_B_ok = false;
is_C_ok = true;
}
}
return NULL;
}
static void* threadFunc_C(voi