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後,子線程的循環條件也不存在就結束了呗