免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6585 | 回复: 5
打印 上一主题 下一主题

[函数] localtime_r在多线程环境下出现死锁 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-24 13:56 |只看该作者 |倒序浏览
#include <pthread.h>
#include <time.h>

void test(char *arg)
{
    pthread_detach(pthread_self());

    struct tm now;
    time_t datetime;

    while (1) {
            time (&datetime);
            localtime_r(&datetime, &now);
            usleep(10);
    }
}

int main()
{
        int rt;
        int tid[7];

        pthread_create(&tid[0], NULL, test, NULL);
        pthread_create(&tid[1], NULL, test, NULL);
        pthread_create(&tid[2], NULL, test, NULL);
        pthread_create(&tid[3], NULL, test, NULL);
        pthread_create(&tid[4], NULL, test, NULL);
        pthread_create(&tid[5], NULL, test, NULL);
        pthread_create(&tid[6], NULL, test, NULL);

        usleep(3000);

        pthread_cancel(tid[0]);
        pthread_cancel(tid[1]);
        pthread_cancel(tid[2]);
        pthread_cancel(tid[3]);

        sleep(1000000);
}

运行大概2秒左右,使用dbx得到如下结果:
(dbx) thread
thread  state-k     wchan            state-u    k-tid mode held scope function
$t1     wait      0xf1000610124a19b0 running  16978061   k   no   sys  nsleep            
$t2     zomb                         terminated 13464059   k   no   sys  pthread_exit      
>$t3     run                          blocked  21594345   k   no   sys  _global_lock_common
$t4     wait      0x000000011000abb8 blocked  21369113   k   no   sys  _rec_mutex_lock   
$t5     wait      0x000000011000abb8 blocked  17006771   k   no   sys  _rec_mutex_lock   
$t6     wait      0x000000011000abb8 blocked  21324271   k   no   sys  _rec_mutex_lock   
$t7     wait      0x000000011000abb8 blocked  21598443   k   no   sys  _rec_mutex_lock   
$t8     wait      0x000000011000abb8 blocked  21332225   k   no   sys  _rec_mutex_lock   

使用truss命令检查,进程一直停在sleep。没有任何localtime_r的函数调用。

我的环境是AIX6.1,目前没有其他的环境,无法进行测试,能帮忙解释一下原因吗?谢谢!

论坛徽章:
0
2 [报告]
发表于 2010-06-24 14:07 |只看该作者
18         localtime_r(&datetime, &now);
19         printf("tid:%u\n", pthread_self());
20         usleep(10);


我的测试,一直在输出:
tid:3023997840
tid:3034487696
tid:3034487696
tid:3044977552
tid:3023997840
tid:3034487696
tid:3044977552
tid:3023997840
tid:3034487696
tid:3044977552
tid:3023997840
tid:3034487696
tid:3023997840
tid:3044977552
tid:3034487696
tid:3023997840
tid:3044977552
tid:3034487696
tid:3034487696
tid:3044977552
tid:3023997840
tid:3034487696
tid:3044977552

论坛徽章:
0
3 [报告]
发表于 2010-06-24 14:08 |只看该作者
没有死锁

论坛徽章:
0
4 [报告]
发表于 2010-06-24 14:16 |只看该作者
楼上两位的测试环境都是AIX6.1嘛?

论坛徽章:
0
5 [报告]
发表于 2010-06-25 14:20 |只看该作者
usleep有bug产生死锁,我在centos上碰到过,没法最后用select代替了usleep

论坛徽章:
0
6 [报告]
发表于 2010-06-25 16:40 |只看该作者
本帖最后由 hankgpg 于 2010-06-25 17:10 编辑

楼主也配叫"wzabcd1234"?  我认识的wzabcd1234 是个非常漂亮的mm, 185cm, 体重160....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP