读书人

linux c 小疑点 但就是出现段异常

发布时间: 2012-09-07 10:38:15 作者: rapoo

linux c 小问题 但就是出现段错误


怎么段错误
*/

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

void * test(void * arg)
{
int i;
for(i = 0;i < 3;i++)
{
sleep(1);
printf("this is in thread\n");
}
pthread_exit(NULL);
}

int main()
{
pthread_t tid;
int j;
tid = pthread_create(&tid,NULL,test,NULL);

pthread_join(tid,NULL);

printf("123\n");
for(j = 0;j<5;j++)
{
printf("this is in mainthread\n");
}

return 0;
}

为什么是断错误??

[解决办法]
用单步调试定位到哪一行出现段错误。
错误应该在这一句:
tid = pthread_create(&tid,NULL,test,NULL);
导致pthread_join(tid,NULL);出错。
你这一句是不对的,pthread_create返回的不是tid,线程创建成功返回0,否则返回错误编号。线程创建成功后,tid经pthread_create后得到了返回值,不需额外赋值。

读书人网 >C语言

热点推荐