zoukankan      html  css  js  c++  java
  • pthread_cond_signal惊群现象

    1.如下代码所示:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    pthread_mutex_t count_lock;
    pthread_cond_t count_ready;
    int count;
    
    void *decrement_count(void *arg)
    {
            while(1)
            {
                    pthread_mutex_lock(&count_lock);
                    printf("decrement:waiting
    ");
                    /*等待满足条件,期间互斥量仍然可用*/
                    //      while (count == 0)
                    pthread_cond_wait(&count_ready, &count_lock);
                    printf("decrement:count = %d
    ",  count);
                    if (count == 0)
                    {
                            printf("exit count:%d
    ",count);
                            exit(1);
                    }
                    count = 0;
                    pthread_mutex_unlock(&count_lock);
            }
            pthread_exit(NULL);
    }
    void *increment_count(void *arg)
    {
            while(1)
            {
                    pthread_mutex_lock(&count_lock);
    //              printf("increment:running
    ");
                    count = 1;
                    /*通知线程条件已满足*/
    //              printf("increment:count = %d
    ",  count);
                    pthread_cond_signal(&count_ready);
                    pthread_mutex_unlock(&count_lock);
            }
            pthread_exit(NULL);
    }
    
    int main()
    {
            pthread_t tid1,tid2,tid3;
            count=0;
            pthread_mutex_init(&count_lock, NULL);
            pthread_cond_init(&count_ready, NULL);
    
            pthread_create(&tid1, NULL, decrement_count, NULL);
            sleep(3);
            pthread_create(&tid3, NULL, decrement_count, NULL);
            sleep(3);
            pthread_create(&tid2, NULL, increment_count, NULL);
            /*等待decrement退出*/
            pthread_join(tid2, NULL);
            printf("decrement quit
    ");
            pthread_join(tid3, NULL);
            pthread_join(tid1, NULL);
            return 0;
    }

    g++ -g thread-cond.cpp -lpthread -o test 编译出test程序。

    然后运行,可见程序

    decrement:waiting
    decrement:waiting
    decrement:count = 1
    decrement:waiting
    decrement:count = 0
    exit count:0

    最后退出了,为什么?

    如果把tid1,tid2,tid3表示为每个线程获得互斥锁,那么这种情况的发生说明tid1和tid3顺序获得锁执行了(顺序也可能为tid3和tid1).

    单从pthread_cond_signal函数的定义上看,如果严格的只发一个"信号"给指定一个线程,这种情况是绝对不可能发生的。

    因为函数中pthread_cond_wait的返回代表了此线程接受到“信号”(pthread_cond_wait执行包括1.解锁2.wait3.获得锁4.返回)

    只有一个原因能解释:pthread_cond_signal一次唤醒了2个wait线程,第1个获得锁的线程把count置为0,第2个线程发现count=0直接exit,

    pthread_cond_signal发生了惊群现象。

    怎么预防:

           while (count == 0)
              pthread_cond_wait(&count_ready, &count_lock);

    在wait返回后加一个while来判断“条件”是否满足要求。
  • 相关阅读:
    Django学习日记04_模板_overview
    Python并发实践_01_线程与进程初探
    web自动化测试笔记(二)
    web自动化测试笔记(一)
    app版本升级的测试点
    移动测(APP)试与web端测试的区别
    Dubbo服务器与普通服务器的区别
    java的错误分类
    安卓手机与iOS手机的区别
    在webstorm里使用git
  • 原文地址:https://www.cnblogs.com/dodng/p/4380035.html
Copyright © 2011-2022 走看看