分享| 22 分钟前150****9045 | 浏览 2 次iWork编程语言 修改标签#include<stdio.h>#include<pthread.h>void a(int a){while(1){printf("123\n");sleep(1);}return; }int main(){pthread_t id;pthread_attr_t attr;int ret,inher;int policy=SCHED_FIFO;struct sched_param param;param.sched_priority=93;pthread_attr_init(&attr);ret=pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);pthread_attr_getinheritsched(&attr,&inher);if(inher==PTHREAD_INHERIT_SCHED)printf("can't change sched policy1\n");else if(inher==PTHREAD_EXPLICIT_SCHED)printf("can change sched policy1\n");#if 1pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);pthread_attr_getinheritsched(&attr,&inher);if(inher==PTHREAD_INHERIT_SCHED)printf("can't change sched policy2\n");else if(inher==PTHREAD_EXPLICIT_SCHED)printf("can change sched policy2\n");ret=pthread_attr_setschedpolicy(&attr,policy);if (ret==0){printf("set policy:%s\n",strerror(ret)); }ret=pthread_attr_setschedparam(&attr,¶m);if (ret==0){printf("set param:%s\n",strerror(ret));}#endifret=pthread_create(&id,&attr,(void *)a,NULL);printf("ret=%d\n",ret);if (ret){printf("pthread_create:%s\n",strerror(ret));}}
1 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
我也碰到同样的问题,我把这一行pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);//注释掉
就好了,而且attr中的属性值也设置到线程里去了。
如果不注释的话,就换成ROOT用户执行,因为修改线程调度策略需要超级用户。
添加回答
举报
0/150
提交
取消