Fair critical section (Linux)

后端 未结 5 1639
没有蜡笔的小新
没有蜡笔的小新 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:24

    If your claim holds true (I haven't got the time to read up, and it would appear as though you have researched this before posting the question), I suggest

     sleep(0);
    

    to explicitely yield in between critical sections.

    while(true)
    {
        critsect.enter();
        ... do calculations ...
        ... maybe call a blocking operation so we sleep ...
        critsect.leave();
        sleep(0);
    }
    

提交回复
热议问题