linux 线程回顾

额,时隔两年重新写博客了。

这次看一下thread_cond_wait(pthread_cond_t * cond, pthread_mutex_t *mutex)和thread_cond_signal(pthread_cond_t *cond);

理解了这两个函数以后,同时对于java中Object的wait()和notify()方法的理解也是会有很多帮助的。

wait中的mutex是用来保护cond,wait的调用者会把wait所在的线程放置在一个线程队列中,这个线程队列等待cond的变化。此过程中的阻塞的并放弃CPU。

wait函数主要做了三件事情:

1、释放之前加过的锁;

2、阻塞并等待cond的变化;

3、返回后在把释放掉的锁再加上;

由此可是调用wait前,一定要有pthead_mutex_lock的相关操作。

而signal将会唤醒线程,值得注意的是signal函数参数只有一个:cond

一般的变成模式如下:

def process:

  pthread_mutex_lock

  while 是否符合条件,不符合进入循环:

    pthread_cond_wait

  process();

  pthread_mutex_unlock

 

def add_meg:

  pthread_mutex_lock

  add()

  pthread_cond_signal

  pthread_mutex_unlock

注意:为什么process中要用while判断条件,wait方法本来就是要阻塞,加上while岂不是多此一举?

答:如果被唤醒以后发现条件不符合,线程并不希望继续向下执行,而是等待条件符合后在执行。

以下代码为简单实例:

 

/*************************************************************************
	> File Name: cond.c
	> Created Time: Tue 02 Sep 2014 11:31:52 PM CST
 ************************************************************************/

#include"local.h"//很多的.h在这里

struct msg{
    struct msg * next;
} ;

struct msg * workq;

pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;

void* pro_msg(){
    struct msg *mp;
    printf("pro_msg\n");
    while(1){
        pthread_mutex_lock(&qlock);
        while(workq == NULL){
            printf("into while and wait\n");
            pthread_cond_wait(&qready, &qlock);
       printf("afte wait\n"); } mp = workq; workq = mp->next; mp->next = NULL; pthread_mutex_unlock(&qlock); } } void* enq(void * mpa){ struct msg * mp = (struct msg *) mpa; pthread_mutex_lock(&qlock); printf("enq\n"); mp->next = workq; workq = mp; pthread_mutex_unlock(&qlock); pthread_cond_signal(&qready); } void main(){ int err; struct msg ww, wq; pthread_t tid1, tid2; workq = NULL; err=pthread_create(&tid1, NULL, enq, (void*)&ww); if(err != 0) printf("error"); err=pthread_create(&tid1, NULL, enq, (void*)&wq); if(err != 0) printf("error"); err=pthread_create(&tid1, NULL, pro_msg, NULL); if(err != 0) printf("error"); pthread_join(tid1, NULL); pthread_join(tid2, NULL); }

 

 

posted on 2014-09-03 23:03  于敬晖  阅读(256)  评论(0编辑  收藏  举报

导航