c++ - pthread_setschedprio() 失败并显示 "EINVAL"

标签 c++ pthreads

我刚刚开始接触pthreads,对此了解不多。我试图使用 pthread_setschedprio() 设置 pthread 的优先级,但它返回“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);
}

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

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

最佳答案

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

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

相关文章:

c - 在 C 中终止线程时的内存泄漏处理

c++ - C++ 中高数的模幂运算

c++ - Apache Thrift,线程安全的 TSimpleServer stop() 调用

c++ - 库中命名空间的使用

c++ - 在负载测试中对 __pthread_mutex_cond_lock_full 断言失败

C - Pthreads 的双重释放错误

c - 生产者消费者同步仅使用互斥体和信号量(无 pthread_cond)

c++ - 如何使用 CMake 查找使用 VS 2017 编译的 Boost 文件系统库?

c++ - 带有 boost 文件系统 directory_iterator 的 Microsoft PPL parallel_for_each

c - 强制线程之间进行通信