pthread_cond_t

本文探讨了并发编程中使用条件变量实现线程间通信的原理及应用,通过实例展示了如何利用条件变量唤醒等待线程,有效管理多线程同步问题。
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>

#define THREAD_NUM_BLOCK 10

pthread_t thread[THREAD_NUM_BLOCK]={0};
pthread_t awake_thread ;
pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con = PTHREAD_COND_INITIALIZER;                                                                                                                                                                                                                                 

void* block(void * arg)
{
     int *index = (int*)(arg);
     pthread_mutex_lock(&mutex);
     printf("thread %d lock on condtion \n",*index);
     pthread_cond_wait(&con,&mutex);
     printf("thread %d awaked \n",*index);
     pthread_mutex_unlock(&mutex);
}

void* send_signal(void * arg)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_broadcast(&con);
    printf("awake all block thread \n");
    pthread_mutex_unlock(&mutex);
}

int main()
{
    int i ;
    for (i=1;i<=THREAD_NUM_BLOCK;i++)
    {
       pthread_create(&(thread[i-1]),NULL,block,&i);
       sleep(1);
    }
    pthread_create(&awake_thread,NULL,send_signal,NULL);
    pthread_join(thread[THREAD_NUM_BLOCK-1],NULL);
    return 0;
}

output:
thread 1 lock on condtion 
thread 2 lock on condtion 
thread 3 lock on condtion 
thread 4 lock on condtion 
thread 5 lock on condtion 
thread 6 lock on condtion 
thread 7 lock on condtion 
thread 8 lock on condtion 
thread 9 lock on condtion 
thread 10 lock on condtion 
awake all block thread 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked 
thread 11 awaked
///////////////////////////////////////////////////////比较
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>

#define THREAD_NUM_BLOCK 10

pthread_t thread[THREAD_NUM_BLOCK]={0};
pthread_t awake_thread ;
pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con = PTHREAD_COND_INITIALIZER;

void* block(void * arg)
{
     long index = (long) arg;
     pthread_mutex_lock(&mutex);
     printf("thread %d lock on condtion \n",index);
     pthread_cond_wait(&con,&mutex);
     printf("thread %d awaked \n",index);
     pthread_mutex_unlock(&mutex);
}

void* send_signal(void * arg)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_broadcast(&con);
    printf("awake all block thread \n");
    pthread_mutex_unlock(&mutex);
}

int main()
{
    long i ;                                                                                                                                                                                                                                                                   
    for (i=1;i<=THREAD_NUM_BLOCK;i++)
    {
       pthread_create(&(thread[i-1]),NULL,block,(void*)i);
       sleep(1);
    }
    pthread_create(&awake_thread,NULL,send_signal,NULL);
    pthread_join(thread[THREAD_NUM_BLOCK-1],NULL);
    return 0;
}
output:
thread 1 lock on condtion 
thread 2 lock on condtion 
thread 3 lock on condtion 
thread 4 lock on condtion 
thread 5 lock on condtion 
thread 6 lock on condtion 
thread 7 lock on condtion 
thread 8 lock on condtion 
thread 9 lock on condtion 
thread 10 lock on condtion 
awake all block thread 
thread 2 awaked 
thread 3 awaked 
thread 4 awaked 
thread 5 awaked 
thread 6 awaked 
thread 7 awaked 
thread 8 awaked 
thread 9 awaked 
thread 10 awaked 
thread 1 awaked

关于signal 和wait:

signal 一定要在wait之后,如何保证。靠开关。

condtion的mutex的作用是什么?为什么一定要和它一起使用。

 spurious wakeup

原因:

在多核处理器下,pthread_cond_signal可能会激活多于一个线程(阻塞在条件变量上的线程)。结果是,当一个线程调用pthread_cond_signal()后,多个调用pthread_cond_wait()或pthread_cond_timedwait()的线程返回。这种效应成为”虚假唤醒”(spurious wakeup) ,虽然虚假唤醒在pthread_cond_wait函数中可以解决,为了发生概率很低的情况而降低边缘条件(fringe condition)效率是不值得的,纠正这个问题会降低对所有基于它的所有更高级的同步操作的并发度。所以pthread_cond_wait的实现上没有去解决它。

解决方法:

while(条件不满足){  
   condition_wait(cond, mutex);  
}  

替换掉

If( 条件不满足 ){  
   Condition_wait(cond,mutex);  
}


转载于:https://my.oschina.net/invictuslee/blog/265682

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值