It seems that the I do not manage to change correctly the priority of my threads with scheduling policy SCHED_FIFO.
The main
thread with fifo priority 10
will launch the thread divisor
and ratio
.
divisor
thread should get priority 2
so that the ratio
thread with priority 1
will not evaluate a/b before b gets a decent value ( this is a completely hypothetical scenario only for the MCVE, not a real life case with semaphores or condition variables ).
Any hint welcome !
Note: environnment under Virtualbox
Normal execution OK
johndoe@VirtualBox:~/Code/gdb_sched_fifo$ g++ main.cc -o main -pthread
johndoe@VirtualBox:~/Code/gdb_sched_fifo$ ./main
Result: 0.333333
Normal execution KO
johndoe@VirtualBox:~/Code/gdb_sched_fifo$ g++ main.cc -o main -pthread
johndoe@VirtualBox:~/Code/gdb_sched_fifo$ ./main
Result: Inf
The MCVE:
#include <iostream>
#include <thread>
double a = 1.0F;
double b = 0.0F;
void ratio(void)
{
struct sched_param param;
param.sched_priority = 1;
pthread_setschedparam(pthread_self(),SCHED_FIFO,¶m);
//error checking not shown for MCVE
std::cout << "Result: " << a/b << "\n" << std::flush;
}
void divisor(void)
{
struct sched_param param;
param.sched_priority = 2;
pthread_setschedparam(pthread_self(),SCHED_FIFO,¶m);
b = 3.0F;
std::this_thread::sleep_for(std::chrono::milliseconds(2000u));
}
int main(int agrc, char * argv[])
{
struct sched_param param;
param.sched_priority = 10;
pthread_setschedparam(pthread_self(),SCHED_FIFO,¶m);
std::thread thr_ratio(ratio);
std::thread thr_divisor(divisor);
thr_ratio.join();
thr_divisor.join();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire