Chinaunix首页 | 论坛 | 博客
  • 博客访问: 523301
  • 博文数量: 80
  • 博客积分: 1496
  • 博客等级: 上尉
  • 技术积分: 1292
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-18 11:24
个人简介

IT码农一个~

文章分类

全部博文(80)

文章存档

2020年(3)

2019年(7)

2017年(1)

2016年(2)

2015年(2)

2014年(26)

2013年(26)

2012年(2)

2011年(1)

2010年(1)

2008年(9)

我的朋友

分类: LINUX

2013-05-27 18:05:18

自己写了一个程序,里面有两个线程,一个生产者,一个消费者。

主程序的关闭是依赖信号 kill,并且会等待一段时间退出。
如果只发送一次关闭信号,程序会等一段正常退出,但是发两次,则程序会陷入死循环。
gdb跟踪,堆栈是在 pthread_cond_signal里面陷入了死锁,查了man,貌似也没说可能会死锁。
然后就查程序。最后终于定位问题。

模拟程序:

 WriteThread  writer;
 writer.start();

    for (int i=0; i < 10; i++)
    {
        writer.notify();
        sleep(3);
    }

    writer.stop();
    writer.notify();
    writer.join();

    // if call pthread_cond_signal on a stopped or destoried cond thread
    // it will cause dead_lock.
    /*
    {
        sleep(1);
        writer.stop();
        writer.notify();
    }
    */

主线程在收到kill信号后,我会立即置消费线程状态为stop, 并唤醒消费线程。
消费线程从 pthread_cond_wait退出,并且销毁 cond变量和mutex变量。

这时候,如果再一次发送kill信号,则又回企图唤醒消费线程,pthread_cond_signal这时候调用的
cond其实已经是destroy过的了。 此时调用线程就死循环了。


The condition functions are not async-signal safe, and should not be called from a signal handler. In particular, calling 
pthread_cond_signal or pthread_cond_broadcast from a signal handler may deadlock the calling thread.


refer: 
阅读(3871) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~