【发布时间】:2015-12-07 11:07:22
【问题描述】:
我有测试代码:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t th_worker, th_worker2;
void * worker2(void *data) {
for(int i = 0; i< 1000000; i++){
printf("thread for worker2----%d\n", i);
usleep(500);
}
}
void * worker(void *data){
pthread_create(&th_worker2, NULL, worker2, data);
for(int i = 0; i< 100; i++){
printf("thread for worker-----%d\n", i);
usleep(500);
}
}
void join(pthread_t _th){
pthread_join(_th, NULL);
}
在 main() 函数中,如果我调用 join(the_worker2):
int main() {
char* str = "hello thread";
pthread_create(&th_worker, NULL, worker, (void*) str);
/* problem in here */
join(th_worker2);
return 1;
}
--> 段错误错误
否则,我打电话:
join(the_worker);
join(th_worker2);
--->好的
为什么在上述情况下会出现段错误? 感谢帮助!!!
【问题讨论】:
标签: c++ c main pthread-join pthread-exit