No se puede provocar la inversión prioritaria en C ++

Estoy tratando de provocar Inversión prioritaria en una pequeñaC ++ programa para fines de demostración, pero no puedo: El subproceso de baja prioridad que contiene el mutex no tiene preferencia y sigue ejecutándose en la sección crítica. Esto es lo que estoy haciendo:

// let's declare a global mutex
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);  // ! None !
  pthread_mutex_init(&my_mutex, &attr);

  // create first normal thread (L):
  pthread_create(&normal_thread, NULL, the_locking_start_routine, NULL);

  // just to help the normal thread enter in the critical section
  sleep(2);

  // now will launch:
  // * (M) several CPU intensive SCHED_FIFO threads with priority < 99
  // * (H) one SCHED_FIFO thread that will try to lock the mutex, with priority < 99

  // build Real Time attributes for the Real Time threads:
  pthread_attr_t my_rt_att;
  pthread_attr_init(&my_rt_att);

  // it was missing in the original post and it was also wrong:
  // even setting the SchedPolicy you have to set "InheritSched"
  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, &params);

  pthread_create(&prio_thread, &my_rt_att, the_CPU_intensive_start_routine, NULL) 

  params.sched_priority = 99;
  pthread_attr_setschedparam(&my_rt_att, &params);

  // create one RealTime thread like this:
  pthread_create(&prio_thread, &my_rt_att, the_locking_start_routine, NULL)  //coma was missing

  ...
}

void *the_locking_start_routine(void *arg) {
  ...
  pthread_mutex_lock(&my_mutex);
  // This thread is on the critical section
  // ... (skipped)
  pthread_mutex_unlock(&my_mutex);
  ...
}

... Pero no funciona, no puedo tener mi Inversión prioritaria deseada.

Esto es lo que pasa

Según tengo entendido, con un programador como el CFS de Linux, un subproceso en tiempo no real (SCHED_OTHER) no se ejecutará hasta que no haya ningún subproceso en tiempo real (SCHED_FIFO o SCHED_RR) en estado de ejecución. Pero he logrado que estos hilos se ejecuten simultáneamente:

(L) Un subproceso en tiempo no real (SCHED_OTHER) que bloquea el mutex y consume CPU (M) varios subprocesos en tiempo real (SCHED_FIFO, y prioridad> 0) CPU intensiva y sin espera para bloquear el mutex (H) Un subproceso en tiempo real (SCHED_FIFO y prioridad más alta) esperando el bloqueo

Hay más subprocesos intensivos de CPU en tiempo real (M) en ejecución que la cantidad de CPU de mi sistema ... pero la retención de subprocesos en tiempo no real (L) el bloqueo todavía consume CPU y termina su trabajo y libera el mutex antes los hilos "M" terminan de consumir CPU.

¿Por qué no se adelanta el subproceso de baja prioridad, la aplicación está bloqueada y no puedo obtener la inversión de prioridad?

Estoy usando g ++ 4.5.2 en Ubuntu Desktop 11.04 con kernel 2.6.38-13.

Respuestas a la pregunta(6)

Su respuesta a la pregunta