线程属性
2020-02-29 本文已影响0人
FakeCSer爱去网吧
typedef struct
{
int etachstate; //线程的分离状态
int schedpolicy; //线程调度策略
struct sched_param schedparam; //线程的调度参数
int inheritsched; //线程的继承性
int scope; //线程的作用域
size_t guardsize; //线程栈末尾的警戒缓冲区大小
int stackaddr_set; //线程的栈设置
void* stackaddr; //线程栈的位置
size_t stacksize; //线程栈的大小
}pthread_attr_t;
对线程属性的设置
属性对象创建销毁,继承性,调度策略,优先权,分离状态,栈
初始化/销毁线程的属性对象
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destory(pthread_attr_t *attr);
使用一个线程对象之前,必须对其进行初始化
线程的继承性
线程的继承性指是否继承创建线程的调度策略和参数
#include <pthread.h>
//获取线程的继承性
int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched);
//设置线程的继承性
int pthread_attr_setinheritsched(pthread_attr_t*attr,int inheritschesd);
继承性inheritsched可取的值:
PTHREAD_INHERIT_SCHED:新进程继承线程的调度策略和参数
PTHREAD_EXPLICIT_SCHED:新进程通过schedpolicy和schedparam显示设置调度策略和调度参数
线程调度策略
#include <pthread.h>
//获取线程的调度策略
int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy)
//设置线程的调度策略
int pthread_attr_setschedpolicy(pthread_attr_t *attr,int *policy)
调度策略policy可取的值:
SCHED_FIFO:先来先服务
SCHED_RR:时间片轮转
SCHED_OTHER:非实时(默认)
线程调度参数
设置线程的优先级
#include <pthread.h>
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);
结构体sched_param定义如下
struct sched_param {
int sched_priority; /* Scheduling priority */
};
优先级设置中,数值越大优先权越高。可用下面两个函数获取最大和最小优选权值
- sched_get_priority_max()
- sched_get_priority_min()
线程分离属性
#include <pthread.h>
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
detachstate的值:
PTHREAD_CREATE_DETACHED
PTHREAD_CREATE_JOINABLE
堆栈信息
- 堆栈大小
#include <pthread.h>
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
stacksize:
PTHREAD_STACK_MIN:默认,最小堆栈大小
自定义:必须大于PTHREAD_STACK_MIN,一般为 STACK_ALIGN的倍数
- 堆栈地址
#include <pthread.h>
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);
- 堆栈信息(堆栈大小和地址)
#include <pthread.h>
int pthread_attr_setstack(pthread_attr_t *attr,
void *stackaddr, size_t stacksize);
int pthread_attr_getstack(pthread_attr_t *attr,
void **stackaddr, size_t *stacksize);
获取已创建属性
#define _GNU_SOURCE
#include <pthread.h>
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr);
示例代码
- 获取(默认)属性信息
//#define _GNU_SOURCE //加上后说重定义了我也不知道为啥
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;
void display(pthread_attr_t *attr)
{
int inheritsched;
int policy;
struct sched_param param;
int detachstate;
size_t stacksize;
void *stackaddr;
pthread_attr_getinheritsched(attr,&inheritsched);
pthread_attr_getschedpolicy(attr,&policy);
pthread_attr_getschedparam(attr,¶m);
pthread_attr_getdetachstate(attr,&detachstate);
pthread_attr_getstack(attr,&stackaddr,&stacksize);
if(inheritsched == PTHREAD_EXPLICIT_SCHED)
{
cout << "inheritsched is PTHREAD_EXPLICIT_SCHED"<<endl;
}else if(inheritsched == PTHREAD_INHERIT_SCHED)
{
cout << "inheritsched is PTHREAD_INHERIT_SCHED"<<endl;
}
if(policy == SCHED_OTHER)
{
cout << "policy is SCHED_OTHER"<<endl;
}
cout << "priority is "<<param.sched_priority<<endl;
if(detachstate==PTHREAD_CREATE_JOINABLE)
{
cout << "detachstate is PTHREAD_CREATE_JOINABLE"<<endl;
}
else if(detachstate==PTHREAD_CREATE_DETACHED)
{
cout << "detachstate is PTHREAD_CREATE_DETACHED"<<endl;
}
cout << "stackaddr is "<<stackaddr<<endl;
cout << "stacksize is "<< stacksize<<endl;
}
void * fun(void *arg)
{
pthread_attr_t attr;
pthread_getattr_np(pthread_self(),&attr);
display(&attr);
}
int main()
{
pthread_t thread;
pthread_attr_t *attr;
pthread_attr_init(attr);
/********set***********/
/********set***********/
pthread_create(&thread,attr,fun,NULL);
pthread_join(thread,NULL);
pthread_attr_destroy(attr);
return 0;
}
运行结果
结果显示
给栈分配了
512个PTHREAD_STACK_MIN(整数倍)