Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which thread would be notified by pthread_cond_signal?

When a thread call pthread_cond_signal(), Unix network programming said pthread_cond_signal() just would nofity just one thread, beacause it isn't pthread_cond_broadcast(). It means there is no race condition. However, the book does not say which thread would be notified, and how. Does the function wake thread randomly?

like image 451
Howo Avatar asked Mar 06 '23 16:03

Howo


1 Answers

Straight from the man:

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

The "scheduling policy" is the order the operating systems decided on. It's one of the four listed in the below link, but you don't really know (without some impressive hackery at least) which one is "first" anyway. It shouldn't matter either - all threads waiting on the condition should be equally ready to continue - otherwise you have a design problem.

Scheduling policies in Linux Kernel has a bit of discussion on some linux policies, and you can google from there if it's important.

like image 175
kabanus Avatar answered Mar 10 '23 10:03

kabanus