线程属性:
attr中包含了以下的多个属性。
初始化与销毁属性:
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);
PTHREAD_CREATE_DETACHED 或者利用pthread_detach()函数。
PTHREAD_CREATE_JOINABLE//默认不是分离的,需要调用pthread_join函数等待退出。
获取与设置栈的大小:
int pthread_attr_setstacksize(pthread_attr_t* attr,size_t stacksize);
int pthread_attr_getstacksize(pthread_attr_t* attr,size_t* stacksize);
获取或设置线程栈末尾警戒区大小
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize);
//获取与设置线程竞争范围
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
int pthread_attr_getscope(const pthread_attr_t *attr, int *scope);
PTHREAD_SCOPE_SYSTEM 进程范围内竞争
PTHREAD_SCOPE_PROCESS 系统范围内竞争
//获取与设置调度策略
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
(相同优先级先进先出调度)SCHED_FIFO, (相同优先级基于抢占式调度,即使优先级相同)SCHED_RR, (默认)SCHED_OTHER
//获取与设置继承调度策略
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched);
PTHREAD_INHERIT_SCHED (主线程与新线程相同策略)
PTHREAD_EXPLICIT_SCHED (调度策略属性需要自己设置)
//获取与设置调度参数
int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);
int pthread_attr_getschedparam(const pthread_attr_t *attr,struct sched_param *param);
struct sched_param {
int sched_priority; // Scheduling priority
};
//获取与设置并发级别。仅在N:M线程模型有效,设置并发级别给内核一个提示:表示提供给定级别数量的核心线程来映射用户线程是高效的。
比如说:用户线程第一级映射成轻量级进程,轻量级进程再一 一映射至核心线程,每个核心线程映射一个CPU。只是提示内核提供多少个轻量级进程是高效的。
int pthread_setconcurrency(int new_level);
int pthread_getconcurrency(void);
1 #include<unistd.h> 2 #include<sys/types.h> 3 #include<fcntl.h> 4 #include<sys/stat.h> 5 #include<stdlib.h> 6 #include<stdio.h> 7 #include<errno.h> 8 #include<pthread.h> 9 #include<string.h> 10 #define ERR_EXIT(m) 11 do 12 { 13 perror(m); 14 exit(EXIT_FAILURE); 15 }while(0 16 int main(void) 17 { 18 pthread_attr_t attr; 19 pthread_attr_init(&attr);//attr中有很多 线程属性的默认值 20 int state; 21 //默认属性是不分离的。要调用pthread_join来防止僵尸 22 pthread_attr_getdetachstate(&attr,&state); 23 if(state==PTHREAD_CREATE_JOINABLE) 24 printf("detachedstate:PTHREAD_CREATE_JOINABLE "); 25 else if(state==PTHREAD_CREATE_DETACHED) 26 printf("detachedstate:PTHREAD_CREATE_DETACHED "); //打印线程默认分离属性。 27 28 size_t size; 29 pthread_attr_getstacksize(&attr,&size); 30 printf("stack size:%d ",size); 31 //获取警戒缓冲区的大小 32 pthread_attr_getguardsize(&attr,&size); 33 printf("guardsize:%d ",size); 34 35 int scope; 36 pthread_attr_getscope(&attr,&scope); 37 if(scope==PTHREAD_SCOPE_PROCESS) 38 printf("scope:PTHREAD_SCOPE_PROCESS "); 39 if(scope==PTHREAD_SCOPE_SYSTEM) 40 printf("scope:PTHREAD_SCOPE_SYSTEM "); 41 //获取与设置调度策略 42 int policy; 43 pthread_attr_getschedpolicy(&attr,&policy); 44 if(policy==SCHED_FIFO) 45 printf("policy:SCHD_FIFO "); 46 else if(policy==SCHED_RR) 47 printf("policy:SCHED_RR "); 48 else if(policy==SCHED_OTHER) 49 printf("policy:SCHED_OTHER "); 50 //获取与设置继承调度策略 51 int inheritsched; 52 pthread_attr_getinheritsched(&attr,&inheritsched); 53 if(inheritsched== PTHREAD_INHERIT_SCHED) 54 printf("inheritsched: PTHREAD_INHERIT_SCHED "); 55 else if(inheritsched==PTHREAD_EXPLICIT_SCHED) 56 printf("inheritsched: PTHREAD_EXPLICIT_SCHED"); 57 //获取与设置调度参数 58 struct sched_param param; 59 pthread_attr_getschedparam(&attr,¶m); 60 //调度优先级 61 printf("sched_priority:%d ",param.sched_priority); 62 63 pthread_attr_destroy(&attr); 64 //并发级别获取 65 int level; 66 level=pthread_getconcurrency(); 67 printf("level:%d ",level); 68 return 0; 69 70 }