开发者

pthread_cond_signal and possible thread starvation

If more then one thread waits on a condition variable, what is the order of waking on a pthread_cond_signal. I have read that pthread_cond_signal does not necessarily wake up in order of sleeping. So this may cause starvat开发者_Python百科ion...is it not?

Thanks


It's determined by the scheduling policy, which is supposed to be fair.

If the threads are not starved by scheduling - they shouldn't be by this either.

From the spec:

If more than one thread is blocked on a condition variable, the scheduling policy determines the order in which threads are unblocked.


Fairness is not guaranteed so starvation is always possible. To ensure absolute fairness, you would need to implement your own mechanism using a queue to keep track of the order of arrivals.


This question suggests abuse of a condition variable. The use of a condition variable is to alert a thread that a condition has occurred. When you wake only one thread, it must be because any thread can service the condition. If you care which thread wakes, then it is not the case that any thread can service the condition.

While you will probably get you what you want on most implementations, there is probably a better way to do whatever it is you are trying to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜