【问题标题】:Equivalent of SetThreadPriority on Linux (pthreads)等效于 Linux 上的 SetThreadPriority (pthreads)
【发布时间】:2012-06-08 05:35:52
【问题描述】:

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

#include <windows.h>

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

【问题讨论】:

    标签: c++ c linux winapi pthreads


    【解决方案1】:

    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。

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

    【讨论】:

    • 您不能将指向未初始化的pthread_attr_t 的指针传递给pthread_attr_getschedpolicy()
    • 请注意,尽管 POSIX 标准(从提供的phtread__setschedprio(3) 手册页到pubs.opengroup.org/onlinepubs/009695399/functions/… 的链接)提到使用pthread_setschedprio(3) 用于在SCHED_OTHER 策略中运行的线程,但在Linux 中的值范围因为优先级值是[0, 0],所以这个答案对 Linux 毫无用处,除非更改为使用问题未调用的实时调度类(SCHED_FIFOSCHED_RR)。
    • 还想指出 pthread_setschedprio 不适用于所有 POSIX 系统(尤其是 BSD)和 OSX:gnu.org/software/gnulib/manual/html_node/… .. 我知道问题是关于 Linux,但我已经遇到过许多运行 Linux 内核的系统,这些系统带有符合 POSIX.1c 的分支 gilbc 和 pthread 库(即 pthreads),但不包含/定义所有 pthread 函数
    【解决方案2】:

    你想要:

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

    【讨论】:

      【解决方案3】:

      正如其他各种答案所提到的,POSIX 标准包括pthread_setschedparam(3)。在谈到实时线程时,大多数情况下都会提到这个 POSIX 线程库函数,但 POSIX 标准并不仅仅将其使用限制在实时线程领域。然而,在 Linux 中,只有在使用实时调度类 SCHED_FIFOSCHED_RR 时,它的使用才真正有意义,因为只有那些调度类允许优先级参数有多个值。请参阅此 stack overflow answer 以获取说明。

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

      如果您决定使用不符合 POSIX 的方式来更改线程的友好性,请注意,有可能有人决定修复提到的不合规性,在这种情况下似乎无法更改线程优先级在 Linux 中,如果使用正常的调度类 (SCHED_OTHER)。

      【讨论】:

        【解决方案4】:

        类似于pthread_setschedparam() 以及策略和优先级的组合。

        我猜你会使用策略SCHED_FIFO, SCHED_RR 来指定线程的优先级。

        【讨论】:

          【解决方案5】:

          对于那些可能正在搜索基于 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;
          }
          

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

          【讨论】:

            猜你喜欢
            • 2011-02-26
            • 1970-01-01
            • 2014-05-14
            • 2020-08-18
            • 1970-01-01
            • 1970-01-01
            • 2018-11-26
            • 2012-11-08
            相关资源
            最近更新 更多