1. detachstate
-
//设置线程的detachstate
-
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
-
//获取线程的detachstate
-
int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);
detach_state有两种:
PTHREAD_CREATE_DETACHED
PTHREAD_CREATE_JOINABLE
joinable:能够被其他线程收回其资源和杀死;在被回收之前,其资源是不释放的
detached:是不能被其他线程回收或杀死的,其资源在终止时由系统自动释放
2.代码:
-
cong@msi:/work/test/thread/5attr$ cat attr.c
-
#include "utils.h"
-
void* read_thread(void* arg)
-
{
-
printf("read_thread\n");
-
return NULL;
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
pthread_t thread_id;
-
pthread_attr_t attr;
-
void* thread_result;
-
int status;
-
-
pthread_attr_init(&attr);
-
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
status = pthread_create(&thread_id, &attr, read_thread, NULL);
-
-
//status = pthread_join(thread_id, &thread_result);
-
dbmsg("thread1 over");
-
return EXIT_SUCCESS;
-
}
5attr.rar (下载后改名为5attr.tar.gz)
阅读(1192) | 评论(0) | 转发(0) |