c++ - 等效于 Linux 上的 SetThreadPriority (pthreads)

标签 c++ c linux winapi pthreads

鉴于以下代码,我想知道在 linux 中假设 pthread 甚至使用 Boost.Thread API 的等效代码是什么。

#include <windows.h>

int main()
{
   SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
   return 0;
}

最佳答案

Linux 中的 SetThreadPriority 等价于 pthread_setschedprio(pthread_t thread, int priority)

查看man page .

编辑:这是等效的示例代码:

#include <pthread.h>

int main()
{
    pthread_t thId = pthread_self();
    pthread_attr_t thAttr;
    int policy = 0;
    int max_prio_for_policy = 0;

    pthread_attr_init(&thAttr);
    pthread_attr_getschedpolicy(&thAttr, &policy);
    max_prio_for_policy = sched_get_priority_max(policy);


    pthread_setschedprio(thId, max_prio_for_policy);
    pthread_attr_destroy(&thAttr);

    return 0;
}

此示例适用于默认调度策略 SCHED_OTHER。

编辑:线程属性必须在使用前初始化。

关于c++ - 等效于 Linux 上的 SetThreadPriority (pthreads),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10876342/

相关文章:

c++ - 当您声明一个 friend 然后在类中定义它时,这意味着什么?

c++ - 重新解释转换行为的 GCC 实现

linux - 是否有 Linux 命令可以将其命令行参数写入文件而不使用 shell?

c - 为什么竞争条件会在循环中发生?

linux - 如何在 Linux 上更改显示器亮度?

C++ udp recvfrom 减少滴

c++ - 第二次迭代崩溃 - 顺序无关

c++ - cin 流位置

c++ - 如何在不属于类的 emacs 中查找 C 函数?

c++ - 学习 C,我的编译器已经知道 bool (Visual Studio 2017)