按序打印 多线程(C和C++)

#include "lib/common.h"

pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;

int testcount = 0;
void block_queue_init()
 {

pthread_mutex_init(&mutex, NULL);
 pthread_cond_init(&cond1, NULL);
 pthread_cond_init(&cond2, NULL);
 pthread_cond_init(&cond3, NULL);
}


void thread_run1(void* arg) {
    int count = 0;
    while (1) {
        pthread_mutex_lock(&mutex);
        while (testcount != 0)
        {
            pthread_cond_wait(&cond1, &mutex);
        }
        testcount++;
printf("1111111111111111\n");
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);

        if (++count >= 3)
        {
            break;
        }
    }
}
void thread_run2(void* arg) {

    int count = 0;
    while (1) {


        pthread_mutex_lock(&mutex);

        while (testcount != 1)
        {
            pthread_cond_wait(&cond2, &mutex);
        }

        testcount++;
printf("2222222222222222\n");
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);

        if (++count >= 3)
        {
            break;
        }
    }
}
void thread_run3(void* arg) {

    int count = 0;
    while (1) {
        pthread_mutex_lock(&mutex);
        while (testcount != 2)
        {
            pthread_cond_wait(&cond3, &mutex);
        }
printf("3333333333333333\n");
        testcount=0;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);

        if (++count >= 3)
        {
            break;
        }
    }
}

int main(int c, char **v) {

    block_queue_init();

    pthread_t thread_tid1;
    pthread_t thread_tid2;
    pthread_t thread_tid3;
    pthread_create(&thread_tid1, NULL, &thread_run1, NULL);
    pthread_create(&thread_tid2, NULL, &thread_run2, NULL);
    pthread_create(&thread_tid3, NULL, &thread_run3, NULL);

    pthread_join(thread_tid1, NULL);
    pthread_join(thread_tid2, NULL);
    pthread_join(thread_tid3, NULL);


pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);


    return 0;
}

#include <iostream>
#include <deque>
#include <thread>
#include <mutex>
#include <condition_variable>

std::deque<int> q;
std::mutex mu;
std::condition_variable cond;
 int testcount = 0;

void function_1() {
    int count = 3;
    while (1) {
        std::unique_lock<std::mutex> locker(mu);
        while(testcount!=0)
            cond.wait(locker); // Unlock mu and wait to be notified

        testcount++;
        std::cout << "aaaaaaaaaaaaaa: " << testcount << std::endl;
        locker.unlock();

        cond.notify_all();  // Notify one waiting thread, if there is one.
        //std::this_thread::sleep_for(std::chrono::seconds(1));

        count--;
        if(count<=0)
        {
        break;
        }
    }
}

void function_2() {
    int count = 3;
    while (1) {
        std::unique_lock<std::mutex> locker(mu);
        while(testcount!=1)
            cond.wait(locker); // Unlock mu and wait to be notified
        testcount++;
        std::cout << "bbbbbbbbbbbbbbbbb: " << testcount << std::endl;
        locker.unlock();

        cond.notify_all();  // Notify one waiting thread, if there is one.
        //std::this_thread::sleep_for(std::chrono::seconds(1));

        count--;
        if(count<=0)
        {
        break;
        }
    }
}

void function_3() {
    int count = 3;
    while (1) {
        std::unique_lock<std::mutex> locker(mu);
        while(testcount!=2)
            cond.wait(locker); // Unlock mu and wait to be notified

        testcount=0;
        std::cout << "ccccccccccccccccc: " << testcount << std::endl;
        locker.unlock();

        cond.notify_all();  // Notify one waiting thread, if there is one.
        //std::this_thread::sleep_for(std::chrono::seconds(1));

        count--;
        if(count<=0)
        {
        break;
        }
    }
}

int main() {
    std::thread t3(function_3);
    std::thread t1(function_1);
    std::thread t2(function_2);

    t1.join();
    t2.join();
t3.join();
    return 0;
}

pthread_cond_signal只能通知一个要通知多个可用c++11 
std::condition_variable cond; cond.notify_all

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值