linux线程回收
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
void pthread_exit(void *retval);
1. 设置线程属性
如果线程创建时,attr属性设置为NULL,那么线程采用默认的属性joinable。需要将属性设置为detached
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id, &attr, func, NULL);
pthread_attr_destroy(&attr);
2. pthread_join
阻塞等待线程退出,并清理(类似于wait, waitpid)
int pthread_join(pthread_t thread, void **retval);
pthread_join(thread, (void**)&thread_ret); //父线程调用
printf("thread_ret = %d.\n", *thread_ret);
3. pthread_detach 非阻塞
int pthread_detach(pthread_t thread);
pthread_detach(pthread_self()); //子线程调用