相当于 Linux 上的 SetThreadPriority (pthreads)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10876342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:38:20  来源:igfitidea点击:

Equivalent of SetThreadPriority on Linux (pthreads)

c++clinuxwinapipthreads

提问by Gelly Ristor

Given the following bit of code, I was wondering what the equivalent bit of code would be in linux assuming pthreads or even using the Boost.Thread API.

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

#include <windows.h>

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

采纳答案by Alexandru C.

The equivalent to SetThreadPriorityin linux would be pthread_setschedprio(pthread_t thread, int priority).

相当于SetThreadPrioritylinux 中的pthread_setschedprio(pthread_t thread, int priority).

Check the man page.

检查手册页

EDIT: here's the sample code equivalent:

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

#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;
}

This sample is for the default scheduling policy which is SCHED_OTHER.

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

EDIT: thread attribute must be initialized before usage.

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

回答by stefanB

Something like pthread_setschedparam()and combination of policy and priority.

类似于pthread_setschedparam()政策和优先级的组合。

I guess you would use policies SCHED_FIFO, SCHED_RRwhere you can specify priority of thread.

我猜您会使用SCHED_FIFO, SCHED_RR可以指定线程优先级的策略。

回答by caf

You want:

你要:

#include <pthread.h>

int main()
{
    int policy;
    struct sched_param param;

    pthread_getschedparam(pthread_self(), &policy, &param);
    param.sched_priority = sched_get_priority_max(policy);
    pthread_setschedparam(pthread_self(), policy, &param);

    return 0;
}

回答by FooF

The POSIX standard includes pthread_setschedparam(3), as mentioned by various other answers. Mostly this POSIX thread library function is mentioned when talking of real-time threads, but the POSIX standard does not limit its use solely to the domain of real-time threads. However, in Linux its use is only really meaningful if using real time scheduling classes SCHED_FIFOor SCHED_RRas only those scheduling classes allow more than one value for the priority parameter. See this stack overflow answerfor an illustration.

POSIX 标准包括pthread_setschedparam(3),如其他各种答案所述。大多数情况下,在谈论实时线程时都会提到这个 POSIX 线程库函数,但 POSIX 标准并没有将其使用仅限于实时线程领域。然而,在 Linux 中,它的使用只有在使用实时调度类时才真正有意义,SCHED_FIFO或者SCHED_RR只有那些调度类允许优先级参数有多个值。有关说明,请参阅此堆栈溢出答案

Fortunately or unfortunately, it is a matter of perspective, it seems both main stream Linux POSIX thread library implementations (the obsolete LinuxThreads and the current NPTL implementation) are not fully POSIX compliant in that the "nice value" is not process specific but thread specific parameter, so it seems you could use setpriority(3)to change the niceness of a thread in Linux. This claim is based on the compatibility notes in pthreads(7)manual page (search for "nice value" in that page); I have not actually tested in practise (straightforward thing to do).

幸运或不幸,这是一个视角问题,似乎主流 Linux POSIX 线程库实现(过时的 LinuxThreads 和当前的 NPTL 实现)都不完全符合 POSIX,因为“好值”不是特定于进程的而是特定于线程的参数,所以看起来你可以setpriority(3)用来改变 Linux 中线程的好坏。此声明基于pthreads(7)手册页中的兼容性说明(在该页面中搜索“nice value”);我还没有在实践中实际测试过(直接做的事情)。

Should you decide to use the POSIX incompliant way of changing the thread niceness, note that there is the lurking possibility that somebody decides to fix the mentioned non-compliance, in which case there seems to be no way of changing the thread priority in Linux if using normal scheduling class (SCHED_OTHER).

如果您决定使用 POSIX 不兼容的方式来改变线程的好坏,请注意,有人决定修复提到的不合规性的潜在可能性,在这种情况下,似乎没有办法改变 Linux 中的线程优先级,如果使用普通调度类 ( SCHED_OTHER)。

回答by blueshogun96

For those who may be searching for BSD based OS solutions such as MacOS or iOS, you may want to consider setting the thread's priority using mach instead of the POSIX equivalent if necessary.

对于那些可能正在搜索基于 BSD 的操作系统解决方案(例如 MacOS 或 iOS)的人,您可能需要考虑使用 mach 而不是 POSIX 等价物来设置线程的优先级(如有必要)。

#include <mach/mach_init.h>
#include <mach/thread_policy.h>
#include <mach/sched.h>
#include <pthread.h>

int set_realtime(int period, int computation, int constraint) {
    struct thread_time_constraint_policy ttcpolicy;
    int ret;
    thread_port_t threadport = pthread_mach_thread_np(pthread_self());

    ttcpolicy.period=period; // HZ/160
    ttcpolicy.computation=computation; // HZ/3300;
    ttcpolicy.constraint=constraint; // HZ/2200;
    ttcpolicy.preemptible=1;

    if ((ret=thread_policy_set(threadport,
        THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&ttcpolicy,
        THREAD_TIME_CONSTRAINT_POLICY_COUNT)) != KERN_SUCCESS) {
            fprintf(stderr, "set_realtime() failed.\n");
            return 0;
    }
    return 1;
}

Source: https://developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html

来源:https: //developer.apple.com/library/content/documentation/Darwin/Conceptual/KernelProgramming/scheduler/scheduler.html