Fair critical section (Linux)

后端 未结 5 1636
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 13:47

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

5条回答
  •  温柔的废话
    2020-12-15 14:32

    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
        }
    }
    

提交回复
热议问题