zoukankan      html  css  js  c++  java
  • 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
    ");
        while(1){
            pthread_mutex_lock(&qlock);
            while(workq == NULL){
                printf("into while and wait
    ");
                pthread_cond_wait(&qready, &qlock);
           printf("afte wait "); } 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 "); 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); }

     

  • 相关阅读:
    bzoj1854 [Scoi2010]游戏
    bzoj2456 mode
    bzoj4810 [Ynoi2017]由乃的玉米田
    bzoj1076 [SCOI2008]奖励关
    bzoj3064 Tyvj 1518 CPU监控
    bzoj1798 [Ahoi2009]维护序列
    bzoj3575 [Hnoi2014]道路堵塞
    bzoj3992 [SDOI2015]序列统计
    uoj#34. 多项式乘法
    高等代数典型问题集
  • 原文地址:https://www.cnblogs.com/yujinghui/p/linux.html
Copyright © 2011-2022 走看看