linux多线程同步pthread_cond_XXX条件变量的理解

  在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-->B-->C,可以create A--->sleep---->create B---->sleep--->create C,但是这未免有点不靠谱,浪费时间不说,如果要求更多,比如要A线程跑起来并且初始化一些条件后,BC才陆续执行,怎么办呢?看到APUE的条件变量这才找到了一个合适的方法。

  条件变量需要于互斥锁结合使用,条件变量的类型是pthread_cond_t,由于条件变量是用在多线程里的,每个线程都可以看到这个变量,通常就把它定义为全局变量。操作条件变量的函数有:

  •   初始化和销毁
SYNOPSIS
#include <pthread.h> int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);  //动态初始化方法,使用完用用destroy释放资源
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;//静态初始化
  •   条件等待
       int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex)

  pthread_cond_timedwait是带超时的等待函数,如果时间到了条件依然没变则返回超时错误,参数里面的mutex就是与之配合使用的互斥量。

  •   条件通知
       int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);

使用的方法:

  这里假定有2个线程,一个等待条件满足,一个改变条件并发出条件改变的通知。等待的线程:

      while (  )
{
pthread_mutex_lock(&mtx);
while ( 条件 == FALSE )
{
pthread_cond_wait(&cond, &mtx);
}
   将条件改变为FALSE
pthread_mutex_unlock(&mtx);
}

  改变条件并通知的线程:

         pthread_mutex_lock(&mtx);
条件 = TRUE;
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&cond);

  需要注意这里代码里的 '条件' 和条件变量 cond是2码事,cond只是用作在线程间传递 '条件' 改变了的一个信使。

  先看看2个模块的流程:

linux多线程同步pthread_cond_XXX条件变量的理解

在pthread_cond_wait函数中,进去前会unlock mtx,等待返回时又会lock mtx。

  分析一下2个线程按随机顺序执行时会怎么样,左边线程叫 A,右边线程叫B,假设 A先lock,这时B就阻塞了,然后A改变条件,解锁,发出通知,由于A接了锁,B马上唤醒,获得锁,那么这时A是无法改变条件的,因为锁被B获得了,B解锁,然后等待,收到通知,B在等待条件队列里面被唤醒,加锁,(这个wait过程按道理要做成原子操作才行,我个人觉得,不然进入wait前的unlock可能又会被A线程抢了锁),B处理完一些事后,解锁,然后不管是A还是B再次获得锁,A都会在解锁后发出通知,B都会在进入等待前解开锁,处理时又锁住,处理完又解锁,也就是说条件变量在需要传递时的通道是被打开的,改变条件变量的过程中又是被封住的。

 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h> static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; struct node
{
int n_number;
struct node *n_next;
} *head = NULL; void *cleanup(void *arg)
{
printf("p:%p\n",arg);
printf("clean up\n");
} void *threadfun(void *arg)
{
struct node *p; pthread_cleanup_push(cleanup, NULL);
while ( )
{
pthread_mutex_lock(&mtx);
while ( head == NULL )
{
pthread_cond_wait(&cond, &mtx);
}
p = head;
head = head->n_next;
pthread_mutex_unlock(&mtx); printf("thread node number is:%d\n",p->n_number);
free(p);
}
pthread_cleanup_pop();
return (void *);
} int main(void)
{
int ret, i;
pthread_t tid;
struct node *p; ret = pthread_create(&tid, NULL, threadfun, NULL);
if ( ret != )
{
perror("pthread_create error\n");
return -;
} for ( i = ; i < ; i++ )
{
p = (struct node *)malloc(sizeof(struct node));
if ( p == NULL )
{
perror("malloc error\n");
continue;
}
memset(p, 0x0, sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx);
p->n_next = head;
head = p;
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&cond);
sleep();
} ret = pthread_cancel(tid);
if ( ret != )
{
printf("pthread_cancel error\n");
} ret = pthread_join(tid, NULL);
if ( ret != )
{
perror("ptread_join error\n");
return -;
} return ;
}

  不过我仍然感觉有问题,如果A线程执行了多次循环,也就是说条件改变了多次,通知了多次,B线程如果跑的慢只执行了一遍,那么通知是否被丢失了?

  这里采用while( 条件 == FALSE )这种结构是有原因的,pthread_cond_signal man上说的是可以唤醒至少1个等待cond的线程,pthread_cond_broadcast 可以唤醒所有等待cond的线程。假设采用pthread_cond_signal,它唤醒了多个线程,然后有一个线程抢到先执行,wait出来后锁住, 改变条件,解锁,等到另一个被唤醒的线程抢到锁时,发现条件依然为FALSE,它就不会再去执行改变条件的操作了,而是继续等待,这样确保signal唤 醒的线程只会有一个执行改变条件的操作。

上一篇:SQL 数据排重,去掉重复数据 有用


下一篇:Qt与JS(三)