c/c++开发分享无法设置Pthread优先级

我无法使用pthread_attr_setschedparam()设置Pthread优先级。 我试图解决这个问题,但无法解决。 我还咨询了我的教科书,该教科书也使用相同的function。 我从书中复制了这段代码。 你能告诉我如何设置线程优先级吗?

这是代码:

 void *Func(void *arg); int main() { pthread_t tid[5]; pthread_attr_t *tattr; struct sched_param param; int pr,error,i; do { if( (tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)) )==NULL) { printf("Couldn't allocate memory for attribute objectn"); } }while(tattr==NULL); if(error=pthread_attr_init(tattr)) { fprintf(stderr,"Attribute initialization failed with error %sn",strerror(error)); } for(i=0;i<5;i++) { //scanf("%d",&pr); error = pthread_attr_getschedparam(tattr,&param); if(error!=0) { printf("failed to get priorityn"); } param.sched_priority=10; error=pthread_attr_setschedparam(tattr,&param); if(error!=0) { printf("failed to set priorityn"); } /* if(i%2==0) { if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_DETACHED)) { fprintf(stderr,"Failed to set thread attributes with error %sn",strerror(error)); } } else if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_JOINABLE)) { fprintf(stderr,"Failed to set thread attributes with error %sn",strerror(error)); } */ pthread_create(&tid[i],tattr,Func,NULL); printf("waiting for thread %dn",i); } free(tattr);// release dynamically allocated memory printf("All threads terminatedn"); return 0; } void *Func(void *arg) { printf("insiden"); pthread_attr_t *tattr=(pthread_attr_t *)arg; int state,error; struct sched_param param; error=pthread_attr_getdetachstate(tattr,&state); if(error==0 && state==PTHREAD_CREATE_DETACHED) { printf(" My state is DETACHEDn"); } else if(error==0 && state==PTHREAD_CREATE_JOINABLE) { printf(" My state is JOINABLEn"); } error=pthread_attr_getschedpolicy(tattr,&param); if(error==0) { printf(" My Priority is %dn",param.sched_priority); } return NULL; } 

    您的pthread_attr_setschedparam调用失败并显示“invalid parameter”。 您的程序将从SCHED_OTHER的默认Linux调度策略开始。 您无法更改SCHED_OTHER的优先级。

    来自man(2) sched_setscheduler

    SCHED_OTHER只能用于静态优先级0. SCHED_OTHER是标准的Linux时间共享调度程序,适用于不需要特殊静态优先级实时机制的所有进程。

    如果在尝试更改程序优先级之前将pthread属性中的策略更改为另一种计划,则程序将起作用。 就像是

     for (i = 0;i < 5;i++) { policy = SCHED_RR; error = pthread_attr_setschedpolicy(tattr, policy); // insert error handling error = pthread_attr_getschedparam(tattr, &param); // insert error handling // yada yada yada ... } 

      I created thread in main and set attributes using pthread_attr_setechedparam(...) .Once the child thread is up and running i want to check this thread priority and policy set before using pthread_getschedparam( , , ). I executed code sudo still it is printing zero for both pri and policy. Please find the code below. #include "stdio.h" #include "stdlib.h" #include "pthread.h" void *Func(void *arg); int main() { pthread_t tid; pthread_attr_t *tattr; struct sched_param param; int pr,error,i; int pol; do { if( (tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)) )==NULL) { printf("Couldn't allocate memory for attribute objectn"); } }while(tattr==NULL); if(error=pthread_attr_init(tattr)) { fprintf(stderr,"Attribute initialization failed with error %sn",strerror(error)); } //for(i=0;i<5;i++) //{ error = pthread_attr_getschedparam(tattr,&param); if(error!=0) { printf("failed to get priorityn"); } int policy=1; error=pthread_attr_setschedpolicy(tattr,policy); if(error!=0) { printf("failed to set priorityn"); } param.sched_priority=10; error=pthread_attr_setschedparam(tattr,&param); if(error!=0) { printf("failed to set priorityn"); } pthread_create(&tid,tattr,Func,NULL); pthread_getschedparam(tid,&pol,&param); printf("policy:: %d pri :: %dn",pol,param.sched_priority); printf("waiting for thread %dn",i); pthread_join(tid,NULL); free(tattr);// release dynamically allocated memory printf("All threads terminatedn"); return 0; } void *Func(void *arg) { printf("insiden"); int policy,err=0; struct sched_param p; err=pthread_getschedparam(pthread_self(),&policy,&p); if(err!=0) { printf("errorn"); } printf("the priorty= %d policy =%dn",p.sched_priority,policy); } 

    这个实现对我有用(有了这个post的帮助),它很简单:

     pthread_t myThread; pthread_attr_t *tattr; struct sched_param param; int error,policy, prio; prio = 10; tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t)); error = pthread_attr_init(tattr); cout << "init: " << error << endl; error = pthread_attr_getschedparam(tattr,&param); cout << "getschedparam: " << error << endl; error = pthread_attr_setinheritsched(tattr, PTHREAD_EXPLICIT_SCHED); cout << "setinheritsched: " << error << endl; policy = SCHED_RR; error = pthread_attr_setschedpolicy(tattr, policy); cout << "setschedpolicy = " << policy << ": " << error << endl; param.sched_priority = prio; error = pthread_attr_setschedparam(tattr ,&param); cout << "setschedparam: " << error << endl; error = pthread_create(&myThread,tattr,threadFunc,&numOfExecutions); cout << "create: " << error << endl; pthread_getschedparam(myThread,&policy,&param); printf("policy:: %d pri :: %dn",policy,param.sched_priority); 

    你可以在这里找到我的工作代码

      以上就是c/c++开发分享无法设置Pthread优先级相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注(猴子技术宅)。

      本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

      如若转载,请注明出处:https://www.ssfiction.com/c-cyuyankaifa/545897.html

      发表评论

      电子邮件地址不会被公开。 必填项已用*标注