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.
#inc
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.
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
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.
You want:
#include <pthread.h>
int main()
{
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(), &policy, ¶m);
param.sched_priority = sched_get_priority_max(policy);
pthread_setschedparam(pthread_self(), policy, ¶m);
return 0;
}
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
).