gpt4 book ai didi

c++ - pthread_setschedprio() 失败,返回 "EINVAL"

转载 作者:搜寻专家 更新时间:2023-10-31 00:48:01 26 4
gpt4 key购买 nike

刚开始接触pthreads,了解不多。我试图使用 pthread_setschedprio() 设置 pthreads 的优先级,但它返回“EINVAL”。这是我的代码:

#include <pthread.h>
#include <errno.h>

void *Run(void *ptr);

class Thread {
public:
pthread_t myPthread;

void start() {
pthread_create( &myPthread, NULL, Run, (void*)this );
}

Thread() { }
virtual void run() = 0;
};

void *Run( void *ptr ) {
Thread* tmp;
tmp = (Thread*)ptr;
tmp->run();
}


class MyThread : public Thread {
public:
virtual void run() {
while (1) {
printf("Running\n");
sleep(1);
}
}
};




MyThread t1;


int main()
{
int status;

t1.start();
status= pthread_setschedprio(t1.myPthread, 300);
if(status!=0)
{
if(status==EPERM)
{
fprintf(stderr,"EPERM\n");
}
else if(status==EINVAL)
{
fprintf(stderr,"EINVAL\n");
}
else
{
fprintf(stderr,"neither EPERM nor EINVAL\n");
}

fprintf(stderr,"error %d\n",status);

errno=status;

perror("");

exit(1);
}
pthread_join( t1.myPthread, NULL);

exit(0);
}

当我尝试编译它时,我得到EINVAL运行错误 22无效参数

谁能告诉我为什么会出现错误以及如何解决?

最佳答案

来自 this链接 它看起来意味着您指定的优先级对于当前调度策略无效。如果你看this页面,您可以确定不同调度策略的有效优先级。最后,如果您不确定,可以使用 pthread_getschedparam 来确定您正在使用的当前调度策略。

关于c++ - pthread_setschedprio() 失败,返回 "EINVAL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com