无法设置Pthread优先级

时间:2012-12-20 03:41:02

标签: c pthreads posix thread-priority

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

以下是代码:

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 object\n");
}
}while(tattr==NULL);

if(error=pthread_attr_init(tattr))
{
    fprintf(stderr,"Attribute initialization failed with error %s\n",strerror(error));
}

for(i=0;i<5;i++)
{
    //scanf("%d",&pr);
        error = pthread_attr_getschedparam(tattr,&param);

        if(error!=0)
        {
            printf("failed to get priority\n");
        }

        param.sched_priority=10;
        error=pthread_attr_setschedparam(tattr,&param);

        if(error!=0)
        {
            printf("failed to set priority\n");
        }
/*  
    if(i%2==0)
    {
        if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_DETACHED))

        {
            fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error));
        }
    }
    else
        if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_JOINABLE))
        {
            fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error));
        }
*/      
        pthread_create(&tid[i],tattr,Func,NULL);


        printf("waiting for thread %d\n",i);
}

free(tattr);// release dynamically allocated memory

printf("All threads terminated\n");
return 0;
} 

 void *Func(void *arg)
{
printf("inside\n");

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 DETACHED\n");
}
else
    if(error==0 && state==PTHREAD_CREATE_JOINABLE)
    {
        printf(" My state is JOINABLE\n");
    }

error=pthread_attr_getschedpolicy(tattr,&param);

if(error==0)
{
    printf(" My Priority is %d\n",param.sched_priority);
}

return NULL;
}

2 个答案:

答案 0 :(得分:5)

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

来自男人(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 ...
}

答案 1 :(得分:1)

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

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 :: %d\n",policy,param.sched_priority);

您可以找到我的工作代码here