1首先我们通过几个函数来了解pthread_attribute吧
/********
属性结构体:
struct attribute
{
int detachstate;
int schepolicy;
struct sched_param schedparam; //调度策略优先级
int inheritsched;
int scope;
size_t guardsize; //线程栈末尾的警戒区缓冲大小
void* stackaddr; //线程站地址
size_t stacksize; //栈大小
}pthread_attr_t
线程属性:优先级,栈大小,继承主线程与否,调度策略,系统绑定
绑定属性:一个用户线程对应一个内核线程,非绑定是由系统分配的,不确定
分离属性:线程以什么方式结束自己
不确定参数直接man -a 函数名
********/
#include
#include
#include
#include
#include
#include
void* thread_fun(void* argc)
{
printf("file=%s,fun=%s,line=%d\n",__FILE__,__FUNCTION__,__LINE__);
}
int main()
{
pthread_t thd,thd2;
struct sched_param schedparam;
int policy, priority, inherit, scope, detachstate,ret;
size_t stacksize;
pthread_attr_t p_attr,p_attr1;
pthread_attr_init(&p_attr);//默认属性
pthread_attr_init(&p_attr1);
char tempbuf[20];
memset(tempbuf,0,20);
/**********************************获取默认线程属性********************************************/
printf("*********************************** gain thread atribute first!*********************\n");
pthread_attr_getschedpolicy(&p_attr,&policy); //调度策略
if(policy == SCHED_RR)
{
strcpy(tempbuf,"SCHED_RR");
}
if(policy == SCHED_FIFO)
{
strcpy(tempbuf,"SCHED_FIFO");
}
if(policy == SCHED_OTHER)
{
strcpy(tempbuf,"SCHED_OTHER");
}
printf("line = %d,tempbuf= %s\n",__LINE__,tempbuf);
priority = sched_get_priority_min(policy); //优先级最小值
printf("line = %d,priority= %d\n",__LINE__,priority);
priority = sched_get_priority_max(policy); //优先级最大值
printf("line = %d,priority= %d\n",__LINE__,priority);
//继承主线程
pthread_attr_getinheritsched(&p_attr,&inherit);
if(inherit == PTHREAD_INHERIT_SCHED)
{
printf("PTHREAD_INHERIT_SCHED:%d\n",inherit);
}
if(inherit == PTHREAD_EXPLICIT_SCHED) //不继承
{
printf("PTHREAD_EXPLICIT_SCHED:%d\n",inherit);
}
//绑定属性
pthread_attr_getscope(&p_attr,&scope);
if(scope == PTHREAD_SCOPE_SYSTEM) //绑定
{
printf("PTHREAD_SCOPE_SYSTEM:%d\n",scope);
}
if(scope = PTHREAD_SCOPE_PROCESS)//不绑定
{
printf("PTHREAD_SCOPE_PTHREAD:%d\n",scope);
}
//分离属性
pthread_attr_getdetachstate(&p_attr,&detachstate);
if(detachstate == PTHREAD_CREATE_DETACHED) //分离
{
printf("PTHREAD_CREATE_DETACHED:%d\n",detachstate);
}
if(detachstate == PTHREAD_CREATE_JOINABLE) //不分离
{
printf("PTHREAD_CREATE_JOINABLE:%d\n",detachstate);
}
//获取栈大小
pthread_attr_getstacksize(&p_attr,&stacksize);
printf("line = %d,stacksize=%ld\n",__LINE__,stacksize);
//优先级
pthread_attr_getschedparam(&p_attr,&schedparam);
printf("schedparam.sched_priority = %d\n",schedparam.sched_priority );
ret = pthread_create(&thd,&p_attr,thread_fun,NULL);
if(ret < 0)
{
printf("file=%s,fun=%s,line=%d return error\n",__FILE__,__FUNCTION__,__LINE__);
}
/*******************************设置线程属性***********************************************/
printf("*********************************** set thread atribute second!*********************\n");
//设置线程属性:
policy = SCHED_FIFO;
pthread_attr_setschedpolicy(&p_attr1,policy);
//获取FIFO优先级范围
priority = sched_get_priority_min(policy); //优先级最小值
printf("line = %d,priority= %d\n",__LINE__,priority);
priority = sched_get_priority_max(policy); //优先级最大值
//继承主线程
inherit = PTHREAD_INHERIT_SCHED;
pthread_attr_setinheritsched(&p_attr1,inherit);
//绑定属性
scope = PTHREAD_SCOPE_SYSTEM;
pthread_attr_setscope(&p_attr1,scope);
//分离属性
detachstate = PTHREAD_CREATE_DETACHED;
pthread_attr_setdetachstate(&p_attr1,detachstate);
//优先级
schedparam.sched_priority = 28;
pthread_attr_setschedparam(&p_attr1,&schedparam);
ret = pthread_create(&thd2,&p_attr1,thread_fun,NULL);
if(ret < 0)
{
printf("file=%s,fun=%s,line=%d return error\n",__FILE__,__FUNCTION__,__LINE__);
}
/********************************获取设置之后线程属性**********************************************/
printf("*********************************** gain thread atribute second(change)!*********************\n");
pthread_attr_getschedpolicy(&p_attr1,&policy); //调度策略
if(policy == SCHED_RR)
{
strcpy(tempbuf,"SCHED_RR");
}
if(policy == SCHED_FIFO)
{
strcpy(tempbuf,"SCHED_FIFO");
}
if(policy == SCHED_OTHER)
{
strcpy(tempbuf,"SCHED_OTHER");
}
printf("line = %d,tempbuf= %s\n",__LINE__,tempbuf);
priority = sched_get_priority_min(policy); //优先级最小值
printf("line = %d,priority= %d\n",__LINE__,priority);
priority = sched_get_priority_max(policy); //优先级最大值
printf("line = %d,priority= %d\n",__LINE__,priority);
pthread_attr_getinheritsched(&p_attr1,&inherit);
if(inherit == PTHREAD_INHERIT_SCHED) //继承主线程
{
printf("PTHREAD_INHERIT_SCHED:%d\n",inherit);
}
if(inherit == PTHREAD_EXPLICIT_SCHED) //不继承
{
printf("PTHREAD_EXPLICIT_SCHED:%d\n",inherit);
}
pthread_attr_getscope(&p_attr1,&scope);//绑定属性
if(scope == PTHREAD_SCOPE_SYSTEM) //绑定
{
printf("PTHREAD_SCOPE_SYSTEM:%d\n",scope);
}
if(scope = PTHREAD_SCOPE_PROCESS)//不绑定
{
printf("PTHREAD_SCOPE_PTHREAD:%d\n",scope);
}
pthread_attr_getdetachstate(&p_attr1,&detachstate); //分离属性
if(detachstate == PTHREAD_CREATE_DETACHED) //分离
{
printf("PTHREAD_CREATE_DETACHED:%d\n",detachstate);
}
if(detachstate == PTHREAD_CREATE_JOINABLE) //不分离
{
printf("PTHREAD_CREATE_JOINABLE:%d\n",detachstate);
}
//获取栈大小
pthread_attr_getstacksize(&p_attr1,&stacksize);
printf("line = %d,stacksize=%ld\n",__LINE__,stacksize);
//优先级
pthread_attr_getschedparam(&p_attr1,&schedparam);
printf("schedparam.sched_priority = %d\n",schedparam.sched_priority );
pthread_attr_destroy(&p_attr);
pthread_attr_destroy(&p_attr1);
pthread_join(thd,NULL);
pthread_join(thd2,NULL);
return 0;
}
然后我们实现以下自己封装一个自己设置优先级,调度策略的线程吧
#include
#include
#include
#include
//#define STACK_SIZE (81920)
int STACK_SIZE = 81920;
typedef void* (*thread_fun)(void*);
//优先级定义
typedef enum sched
{
SCHEID_FIFO = 0,
SCHEID_RR,
SCHEID_OTHER //分时调度
}SCHED_PRIOTY;
创建我的线程
int ros_pthread_create(pthread_t* pid,int schem_method,int privaty,int stacksize,thread_fun threadfun,void* argc)
{
pthread_attr_t attr;
struct sched_param schedparam;
int priority;
int ret = 0;
memset(&attr,0,sizeof(pthread_attr_t));
//pthread_attr_init(&attr);
//设置线程属性:FIFO RR OTHER
ret = pthread_attr_setschedpolicy(&attr,schem_method);
//获取FIFO优先级范围
priority = sched_get_priority_min(schem_method); //优先级最小值
printf("line = %d,priority= %d\n",__LINE__,priority);
priority = sched_get_priority_max(schem_method); //优先级最大值
printf("line = %d,priority= %d\n",__LINE__,priority);
//优先级
schedparam.sched_priority = privaty;
ret = pthread_attr_setschedparam(&attr,&schedparam);
//栈大小
ret = pthread_attr_setstacksize(&attr,stacksize);
if (ret < 0)
{
printf(" fun = %s line = %d return error\n",__FUNCTION__,__LINE__);
return -1;
}
//创建线程
ret = pthread_create(pid,&attr,threadfun,argc);
if(ret < 0)
{
printf(" fun = %s line = %d return error\n",__FUNCTION__,__LINE__);
return -1;
}
printf(" fun = %s create success\n",__FUNCTION__);
}
void* my_fun1(void* a)
{
while(1)
{
printf(" fun1 = %s line = %d return\n",__FUNCTION__,__LINE__);
}
}
void* my_fun2(void* a)
{
while(1)
{
printf(" fun2 = %s line = %d return\n",__FUNCTION__,__LINE__);
}
}
int main()
{
pthread_t pid1,pid2;
int ret = ros_pthread_create(&pid1,(int)SCHEID_FIFO,1,STACK_SIZE,my_fun1,NULL);
if(ret < 0)
{
printf(" fun = %s line = %d return error\n",__FUNCTION__,__LINE__);
return -1;
}
ret = ros_pthread_create(&pid2,(int)SCHEID_FIFO,99,STACK_SIZE,my_fun2,NULL); //数字越小,优先级越高
if(ret < 0)
{
printf(" fun = %s line = %d return error\n",__FUNCTION__,__LINE__);
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid1,NULL);
}