标签:
1 //case 1:创建线程时设置为Detach,即unjoinable 2 pthread_t tid; 3 pthread_attr_t attr; 4 pthread_attr_init(&attr); 5 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); 6 pthread_create(&tid, &attr, (void *)&thread_function, NULL); 7 8 //case 2:在线程中调用pthread_detach 9 void thread_function(void *p) 10 { 11 pthread_detach(pthread_self()); 12 } 13 14 //case 3: 在创建线程后调用pthread_detach 15 pthread_create(&tid, NULL, (void *)&thread_function, NULL); 16 pthread_detach(tid);
2. 当需要关注线程状态时,需要调用pthread_join。
1 pthread_create(&tid, NULL, (void *)&thread_function, NULL); 2 pthread_join(thread_id, NULL);
标签:
原文地址:http://www.cnblogs.com/zSir-2015/p/4909829.html