I am trying to call Priority Inversion in a small C ++ program for demo purposes, but I can’t: the low priority thread containing the mutex is not unloaded and continues to work in the critical section. This is what I do:
pthread_mutex_t my_mutex;
...
int main(int argc, char **argv) {
...
pthread_t normal_thread;
pthread_t prio_thread;
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_NONE);
pthread_mutex_init(&my_mutex, &attr);
pthread_create(&normal_thread, NULL, the_locking_start_routine, NULL);
sleep(2);
pthread_attr_t my_rt_att;
pthread_attr_init(&my_rt_att);
pthread_attr_setinheritsched(&my_rt_att, PTHREAD_EXPLICIT_SCHED)
pthread_attr_setschedpolicy(&my_rt_att, SCHED_FIFO);
struct sched_param params;
params.sched_priority = 1;
pthread_attr_setschedparam(&my_rt_att, ¶ms);
pthread_create(&prio_thread, &my_rt_att, the_CPU_intensive_start_routine, NULL)
params.sched_priority = 99;
pthread_attr_setschedparam(&my_rt_att, ¶ms);
pthread_create(&prio_thread, &my_rt_att, the_locking_start_routine, NULL)
...
}
void *the_locking_start_routine(void *arg) {
...
pthread_mutex_lock(&my_mutex);
pthread_mutex_unlock(&my_mutex);
...
}
... But this will not work, I cannot get the desired priority inversion.
Here's what happens:
As I understand it, with a scheduler like Linux CFS, the non-real-time stream (SCHED_OTHER) will not work until there is a Real Time stream (SCHED_FIFO or SCHED_RR) in runnning mode. But I reached these threads at the same time:
- (L) One non-real-time thread (SCHED_OTHER) blocking the mutex and consuming processor
- (M) (SCHED_FIFO > 0) CPU
- (H) (SCHED_FIFO )
(M), ... (L) - mutex , "M" .
, ?
g++ 4.5.2 Ubuntu Desktop 11.04 2.6.38-13.