According to this answer the default library for C ++ 11 does not provide standard support for thread priority control. (The author still believes that this will not change in C ++ 14)
In this same answer he cites a command that works on systems that follow the rules POSIX :
pthread_setschedparam(thread.native_handle(), politica, {prioridade});
As you only want for linux, this method should solve your problem. There are still some relevant points to take into account.
Linux's default thread policy has dynamic priority
Generally, when you start a thread, Linux sets the SCHED_OTHER
policy, as seen in this SOEN response .
In this same answer, it puts the policy types that can be adopted for the thread system and which is the minimum and maximum priority:
SCHED_FIFO
: Queue schema, first to enter, is the first to exit. (1/99)
SCHED_RR
: Policy scheme round-robin . (1/99)
Where the priority is as follows (min / max). I chose to put the policies that had priority. Although I read in the comments that SCHED_OTHER
can provide a certain level of priority control , it is set by the system itself according to the behavior of the thread, what you can do is give" a hint of importance of the thread "setting its priority as too high ( -20) or very low (19).
Threading Exchange Policies
Threads with policies SCHED_RR
or SCHED_FIFO
will be exchanged if one of the two events happens, still according to this link :
- A thread is either sleeping (
sleep
) or waiting for an event
- A higher priority real-time thread is ready to run
These points should be taken into account when you implement your threads.
Having said that, let's go to our example:
Example taken from cpp reference :
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <pthread.h>
std::mutex iomutex;
void f(int num) {
std::this_thread::sleep_for(std::chrono::seconds(1));
sched_param sch;
int policy;
pthread_getschedparam(pthread_self(), &policy, &sch);
std::lock_guard<std::mutex> lk(iomutex);
std::cout << "Thread " << num << " esta executando com prioridade "
<< sch.sched_priority << '\n';
}
int main(){
//A thread 2 será uma thread padrão
std::thread t1(f, 1), t2(f, 2);
sched_param sch;
int policy;
pthread_getschedparam(t1.native_handle(), &policy, &sch);
sch.sched_priority = 20;
//Nessa linha ele seta a política e a prioridade da thread 1
if(pthread_setschedparam(t1.native_handle(), SCHED_FIFO, &sch)) {
std::cout << "Falha para utilizar setschedparam: " << std::strerror(errno) << '\n';
}
t1.join(); t2.join();
}
In spite of everything, I was left with a doubt, perhaps because of my lack of knowledge of how POSIX systems handle thread priorities, but, as the example puts, threads with higher values priority, minor priority.
Example output:
Thread 2 esta executando com prioridade 0
Thread 1 esta executando com prioridade 20