20

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.

#include <windows.h>

int main()
{
   SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
   return 0;
}
Gelly Ristor
  • 573
  • 1
  • 6
  • 17

5 Answers5

29

The equivalent to SetThreadPriority in linux would be 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.

EDIT: thread attribute must be initialized before usage.

Alexandru C.
  • 3,337
  • 1
  • 25
  • 27
  • 1
    You can't pass a pointer to an uninitialised `pthread_attr_t` to `pthread_attr_getschedpolicy()`. – caf Jun 06 '12 at 06:45
  • 10
    Note that although the POSIX standard (following the link from the provided `phtread__setschedprio(3)` manual page to http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_08.html#tag_02_08_04_01) mentions using `pthread_setschedprio(3)` for threads running in `SCHED_OTHER` policy, in Linux the value range for the priority value is `[0, 0]` making this answer useless for Linux, unless changed to use real time scheduling classes (`SCHED_FIFO` or `SCHED_RR`) which is uncalled by the question. – FooF Jan 13 '14 at 07:36
  • 2
    Also wanted to note that `pthread_setschedprio` isn't available for all POSIX systems (especially the BSD's) and OSX: https://www.gnu.org/software/gnulib/manual/html_node/pthread_005fsetschedprio.html .. I know the question is about `Linux` specifically, but I've run into many a system that run the Linux kernel with an offshoot gilbc and pthread library that are POSIX.1c compliant (i.e. pthreads), but don't include/define all of the pthread functions – txtechhelp Jun 23 '15 at 21:44
20

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;
}
caf
  • 233,326
  • 40
  • 323
  • 462
7

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_FIFO or SCHED_RR as only those scheduling classes allow more than one value for the priority parameter. See this stack overflow answer for an illustration.

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).

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).

Community
  • 1
  • 1
FooF
  • 4,323
  • 2
  • 31
  • 47
3

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

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

stefanB
  • 77,323
  • 27
  • 116
  • 141
2

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.

#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

blueshogun96
  • 212
  • 2
  • 6