pthread_attr_t就是对应线程的属性
bash
/*
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr);
初始化线程属性变量
int pthread_attr_destroy(pthread_attr_t *attr);
释放线程属性资源
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
获取线程分离的状态属性
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
设置线程分离的状态属性
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void* callback(void* arg) {
printf("child thread id:%ld", pthread_self());
return NULL;
}
int main() {
//创建一个线程属性变量
pthread_attr_t attr;
//初始化
pthread_attr_init(&attr);
//设置属性
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
//创建一个子线程
pthread_t tid;
int ret = pthread_create(&tid, &attr, callback, NULL);
if(ret != 0) {
char* strerr = strerror(ret);
printf("error: %s\n", strerr);
}
printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());
size_t size;
pthread_attr_getstacksize(&attr, &size);
printf("%ld\n", size);
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
}