线程优先级许多讲解都是Java方面的。 设置优先级有两种方法,
一旦决定这么做之间就要准备几个步骤。
pthread_attr_t attr; //要声明的结构体
typedef struct { int detachstate; 线程的分离状态 int schedpolicy; 线程调度策略 struct sched_param schedparam; 线程的调度参数 int inheritsched; 线程的继承性 int scope; 线程的作用域 size_t guardsize; 线程栈末尾的警戒缓冲区大小 int stackaddr_set; void * stackaddr; 线程栈的位置 size_t stacksize; 线程栈的大小 }pthread_attr_t;struct sched_param param; //同上
struct sched_param { int sched_priority; //所要设定的线程优先级,就当于数字。 };好了现在我们知道了两个结构体都是干什么的了。 pthread_attr_init(&attr); //初始化结构体attr pthread_attr_setschedpolicy(&attr, SCHED_RR); //开始设置attr这个对象了,sched_rr不知道的区百度。 param.sched_priority = 10;//这个就是调度参数 pthread_attr_setschedparam(&attr, ¶m); //然后设置调度参数。 pthread_create(xxx , &attr , xxx , xxx); //创建线程,然后就结束了 pthread_attr_destroy(&attr); //此处是销毁初始化。
还有利用shell命令方式提高优先级的,我这里只讨论程序代码方面的。