线程同步,互斥量与条件变量,两个线程交叉打印数据

利用互斥锁与条件变量实现了两个线程交叉打印数据

#include 
#include 
#include 

using namespace std;

bool flag = true;

class Producer {
public:
    static void* A(void* arg)
    {
        while (1) {
            while (!flag) {
                pthread_cond_wait(&cond, &mutex);
            }
            cout << "A" << endl;
            flag = false;
            sleep(1);
            pthread_cond_signal(&cond);
        }
    }

    static void* B(void* arg)
    {
        while (1) {
            while (flag) {
                pthread_cond_wait(&cond, &mutex);
            }
            cout << "B" << endl;
            flag = true;
            sleep(1);
            pthread_cond_signal(&cond);
        }
    }
    static pthread_mutex_t mutex;
    static pthread_cond_t cond;
};

pthread_mutex_t Producer::mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t Producer::cond = PTHREAD_COND_INITIALIZER;

int main()
{
    pthread_t tid[2];
    pthread_create(&tid[0], NULL, Producer::A, NULL);
    pthread_create(&tid[1], NULL, Producer::B, NULL);
    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

}

 

你可能感兴趣的:(c++,linux系统编程与网络编程,Linux内核与操作系统)