C语言中pthread_cond_wait 详解

一、pthread_cond_wait函数的原理

  1. pthread_cond_wait()
    用于阻塞当前线程,等待别的线程使用pthread_cond_signal()pthread_cond_broadcast来唤醒它 pthread_cond_wait() 必须与pthread_mutex配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread_cond_signal()pthread_cond_broadcast,把该线程唤醒,使pthread_cond_wait()通过(返回)时,该线程又自动获得该mutex
  2. pthread_cond_signal函数的作用是发送一个信号给另外一个正在处于阻塞等待状态的线程,使其脱离阻塞状态,继续执行.如果没有线程处在阻塞等待状态,pthread_cond_signal也会成功返回。
    使用pthread_cond_signal一般不会有“惊群现象”产生,他最多只给一个线程发信号。假如有多个线程正在阻塞等待着这个条件变量的话,那么是根据各等待线程优先级的高低确定哪个线程接收到信号开始继续执行。如果各线程优先级相同,则根据等待时间的长短来确定哪个线程获得信号。但无论如何一个pthread_cond_signal调用最多发信一次。
  3. 但是pthread_cond_signal在多处理器上可能同时唤醒多个线程,当你只能让一个线程处理某个任务时,其它被唤醒的线程就需要继续
    wait,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程. 另外,某些应用,如线程池,pthread_cond_broadcast唤醒全部线程,但我们通常只需要一部分线程去做执行任务,所以其它的线程需要继续wait.所以强烈推荐对pthread_cond_wait()
    使用while循环来做条件判断.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

//链表的结点
struct msg
{
    int num; 
    struct msg *next; 
};
 
struct msg *head = NULL;    //头指针
struct msg *temp = NULL;    //节点指针

//静态方式初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t has_producer = PTHREAD_COND_INITIALIZER;
 
void *producer(void *arg)
{
    while (1)   //线程正常不会解锁,除非收到终止信号
	{
        pthread_mutex_lock(&mutex);         //加锁

        temp = malloc(sizeof(struct msg));
        temp->num = rand() % 100 + 1;
        temp->next = head;
        head = temp;                        //头插法
        printf("---producered---%d\n", temp->num);

        pthread_mutex_unlock(&mutex);       //解锁
        pthread_cond_signal(&has_producer); //唤醒消费者线程
        usleep(rand() % 3000);              //为了使该线程放弃cpu,让结果看起来更加明显。
    }
 
    return NULL;
}
 
void *consumer(void *arg)
{
    while (1)       //线程正常不会解锁,除非收到终止信号
	{
        pthread_mutex_lock(&mutex);     //加锁
        while (head == NULL)            //如果共享区域没有数据,则解锁并等待条件变量
	    {
            pthread_cond_wait(&has_producer, &mutex);   //我们通常在一个循环内使用该函数
        }
        temp = head;
        head = temp->next;
        printf("------------------consumer--%d\n", temp->num);
        free(temp);                     //删除节点,头删法
        temp = NULL;                    //防止野指针
        pthread_mutex_unlock(&mutex);   //解锁

        usleep(rand() % 3000);          //为了使该线程放弃cpu,让结果看起来更加明显。
    }
 
    return NULL;
}
 
int main(void)
{
    pthread_t ptid, ctid;
    srand(time(NULL));      //根据时间摇一个随机数种子

    //创建生产者和消费者线程
    pthread_create(&ptid, NULL, producer, NULL);
    pthread_create(&ctid, NULL, consumer, NULL);

    //主线程回收两个子线程
    pthread_join(ptid, NULL);
    pthread_join(ctid, NULL);
 
    return 0;
}
  • 36
    点赞
  • 176
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
pthread_cond_timedwait函数是一个等待特定条件变量的函数,它允许线程在指定时间内等待条件变量的满足。函数原型如下: ```c int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); ``` 参数说明: - cond:指向条件变量的指针。 - mutex:指向互斥锁的指针。在调用该函数前,必须已经持有这个互斥锁。 - abstime:指向保存等待时间的timespec结构体的指针,表示等待的绝对时间。 函数返回值为0表示成功,非零值表示失败。 以下是一个使用pthread_cond_timedwait的示例代码: ```c #include <stdio.h> #include <pthread.h> #include <time.h> pthread_mutex_t mutex; pthread_cond_t cond; void* thread_func(void* arg) { printf("Child thread waiting for condition...\n"); struct timespec wait_time; clock_gettime(CLOCK_REALTIME, &wait_time); wait_time.tv_sec += 5; // 等待5秒钟 pthread_mutex_lock(&mutex); int ret = pthread_cond_timedwait(&cond, &mutex, &wait_time); if (ret == 0) { printf("Condition signaled!\n"); } else if (ret == ETIMEDOUT) { printf("Timeout occurred!\n"); } else { printf("Error occurred while waiting for condition\n"); } pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t tid; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&tid, NULL, thread_func, NULL); sleep(3); // 保证子线程在等待之前先运行 pthread_mutex_lock(&mutex); printf("Main thread signaling condition...\n"); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); pthread_join(tid, NULL); pthread_cond_destroy(&cond); pthread_mutex_destroy(&mutex); return 0; } ``` 上述代码,主线程创建了一个子线程,子线程调用pthread_cond_timedwait函数等待条件变量的满足。主线程在等待3秒后发出信号,并通知子线程条件已满足。如果在等待时间内收到信号,则打印"Condition signaled!",否则打印"Timeout occurred!"。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值