On a multi-threaded Linux application I use a mutex for critical sections. This works very well except for the fairness issue. It can happen that a thread leaving a critical
IMHO you can use a FIFO SCHEDULER on Linux and change de priority of threads:
thread_func() {
...
pthread_t t_id = pthread_self();
struct sched_param prio_zero, prio_one;
prio_zero.sched_priority = sched_get_priority_min(SCHED_FIFO);
prio_one.sched_priority = sched_get_priority_min(SCHED_FIFO) + 1;
phtread_setschedparam(t_id, SCHED_FIFO, &prio_zero);
...
while(true)
{
... Doing something before
phtread_setschedparam(t_id, SCHED_FIFO, &prio_one);
critsect.enter();
... do calculations ...
... maybe call a blocking operation so we sleep ...
critsect.leave();
phtread_setschedparam(t_id, SCHED_FIFO, &prio_zero);
... Do something after
}
}