[Linux]多线程同步之pthread_cond_wait()学习笔记

线程间同步的一种情况:线程A需要等某个条件成立才能继续往下执行,现在这个条件不成立,线程A就阻塞等待,而线程B在执行过程中使这个条件成立了,就唤醒线程A继续执行。

pthread库中通过条件变量(Condition Variable)来阻塞等待一个条件,或者唤醒等待这个条件的线程 。


一个生产者-消费者的例子:消费者A等待生产者B产出产品后才打印,否则消费者阻塞等待生产者生产。


#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

struct msg {
    struct msg *next;
    int num;
};

struct msg *head;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *producer(void *p)
{
    struct msg *mp;
    while(1){
       mp =  malloc(sizeof(struct msg));
       mp->num = rand() % 1000 + 1;
       printf("produce %d\n", mp->num);
     
       pthread_mutex_lock(&lock);
       mp->next = head;
       head = mp;       
       pthread_mutex_unlock(&lock);
       pthread_cond_signal(&has_product);       
       sleep(2);
    }
}

void *consumer(void *p)
{
    while(1){
        pthread_cond_wait(&has_product, &lock);
    
        struct msg *tmp = head;
        while(tmp != NULL) {
            printf("%d ", tmp->num);
                tmp = tmp->next;
        }
        printf("\n");
    }
}


int main()
{
    pthread_t pid, cid;

    pthread_create(&pid, NULL, producer, NULL);
    pthread_create(&cid, NULL, consumer, NULL);

    pthread_join(pid, NULL);
    pthread_join(cid, NULL);

    return 0;
}

运行结果:




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
pthread_cond_broadcast和pthread_cond_wait也是用于线程间同步的函数,类似于pthread_cond_signal和pthread_cond_wait的组合,但有一些区别。 pthread_cond_broadcast用于广播条件变量的信号。当一个线程调用pthread_cond_broadcast时,它会唤醒所有正在等待这个条件变量的线程。这与pthread_cond_signal的区别在于,pthread_cond_signal只会唤醒一个等待线程,而pthread_cond_broadcast会唤醒所有等待线程。 pthread_cond_wait用于等待条件变量的信号,与pthread_cond_signal和pthread_cond_broadcast一起使用。当一个线程调用pthread_cond_wait时,它会阻塞等待条件变量的信号。当收到信号后,线程会重新激活,并且会重新检查条件是否满足。如果条件不满足,线程可能会再次进入等待状态。 以下是一个使用pthread_cond_broadcast和pthread_cond_wait的示例代码: ```c pthread_mutex_t mutex; pthread_cond_t cond; int condition = 0; void* thread1(void* arg) { pthread_mutex_lock(&mutex); while (condition == 0) { pthread_cond_wait(&cond, &mutex); } // 条件满足后执行的代码 pthread_mutex_unlock(&mutex); return NULL; } void* thread2(void* arg) { pthread_mutex_lock(&mutex); condition = 1; pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t tid1, tid2; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&tid1, NULL, thread1, NULL); pthread_create(&tid2, NULL, thread2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } ``` 在上述示例thread1线程调用pthread_cond_wait等待条件满足,而thread2线程在某个时刻将条件设置为满足,并调用pthread_cond_broadcast发送信号。这样,所有等待的线程都会被唤醒并执行相应的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值