文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>pthread_join引发的segmentation fault

pthread_join引发的segmentation fault

时间:2010-08-17  来源:shinyguo_s

先来看一段代码

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>


void *fun(void *arg)
{
    int    i ;    

    for (i=0; i<5; i++) {
        printf("hello%d\n", i);
    }
    return NULL;
}

int main()
{
    pthread_t    thread;
   
    thread = pthread_create(&thread, NULL, &fun, NULL);
    if (thread != 0) {
        printf("error");
        exit(1);
    }
    pthread_join(thread, NULL);

    return 0;
}

如果你能一眼就看出来这个程序是错误的,那么我太佩服你了!因为这个纠缠了半个下午。最开始的程序不是这样的,我调试的时候把没用的语句都删除了,但还是出现segmentation fault.崩溃了要。找来同学一起研究,一下子也没看出来,按说是非常简单的一个程序啊,创建,运行,结束,等待结束,over。我还在想是不是thread是C的关键字冲突了?
正当要给thread改名的时候突然发现:

thread = pthread_create(&thread, NULL, &fun, NULL);

怎么能把创建线程的结果赋给thread本身呢?!如果创建成功, pthread_create将返回0. 这样thread就变成0了。等到结束时执行pthread_join(0, NULL),哪儿有0号线程啊,失败,段错误。
好幼稚的一个bug....不过真的很崩溃
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载