c++多线程循环输出ABCD

#include 
#include 
#include 
#include 
#include 
#include 

#define THRNUM 4
static int num=0;
std::mutex m;
std::condition_variable cond;

using namespace std;
int next(int n)
{
    if(n +1 ==THRNUM)
        return 0;
    return n+1;
}

void fun(int i)
{

    while(1)
    {
        std::unique_lock lk(m);
        while(num != i)
            cond.wait(lk,
                      [=]{return num != i ?false :true;});
        int c = 'A'+num;
        write(1,&c,1);
        num = next(num);
        lk.unlock();
        cond.notify_all();
    }

}


int main(int argc, char *argv[])
{
    std::thread t[4];
    for(int i = 0;i

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