Lipika Deka
Lipika Deka

Reputation: 3884

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 starvation...is it not?

Thanks

Upvotes: 2

Views: 868

Answers (3)

David Schwartz
David Schwartz

Reputation: 182769

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.

Upvotes: 0

Tudor
Tudor

Reputation: 62439

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.

Upvotes: 1

littleadv
littleadv

Reputation: 20272

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.

Upvotes: 4

Related Questions