include
#include
#include
static int run = 1;
static int retvalue;
void *start_routine(void *arg){
int *runing = arg;
printf("The child thread has been inited,the arg is: %d\n",*runing);
while(*runing){
printf("The child thread is runing !\n");
usleep(1);
}
printf("Exit child thread\n");
retvalue = 8;
pthread_exit((void *)&retvalue);
}
int main(void){
pthread_t pt;
int ret = -1;
int times = 3;
int i = 0;
int ret_join = NULL;
ret = pthread_create(&pt,NULL,(void)start_routine,&run);
if(ret !=0){
printf("Create thread fail!\n");
return 1;
}
usleep(1);
for(;i < times;i++){
printf("Print the main thread!\n");
usleep(1);
}
run = 0;
pthread_join(pt,(void*)&ret_join);
printf("The return of thread is %d\n",*ret_join);
return 0;
}
在子线程中,while循环是一个死循环,那个子线程如何退出终止的?各位大神,求解答!谢了各位!总是在线程这里玩儿不明白...
哥们,子线程哪是一个死循环呢。。。 子线程循环条件就是run>0, 程序开始运行后,因为usleep()作用在主线程和线程之间切换执行,主线程执行到run=0后,子线程的循环条件也不存在就结束了呗
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。