对于优先级设置的总结

it2024-10-10  36

线程优先级,我工作中遇到的第一个问题

线程优先级许多讲解都是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, &param); //然后设置调度参数。 pthread_create(xxx , &attr , xxx , xxx); //创建线程,然后就结束了 pthread_attr_destroy(&attr); //此处是销毁初始化。

第二种:在线程运行时动态的创建。
tid1=pthread_self();//得到线程id printf("pthread ID is %d\n",tid1); ret = pthread_setschedprio(tid1, 60);//直接赋值修改。0-255,数字越大优先级越高. if(ret != 0) { perror("func pthread_setschedprio error"); return (PVOID)-1; }

还有利用shell命令方式提高优先级的,我这里只讨论程序代码方面的。

最新回复(0)