9

notify_one() of C++ 11 thread library is used to unblock one of the waiting threads.

How does it choose which thread to unblock?

To begin with, I tried googling but could not find any appropriate answer (even on Stack Exchange network), though I read somewhere that the selection of thread is "unspecified".

So I decided to checkout the C++ specification and referred Section 30.5.1 of November 2014 working draft (available for free), it said

Effects: If any threads are blocked waiting for *this, unblocks one of those threads.

Clearly, even the specification does not mention what the selection is based on, then I decided to put up this question.

2
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Jul 3, 2015 at 11:21
  • 1
    @gnat To begin with, I tried googling but could not find any appropriate answer (even on stack exchange network), though I read somewhere that the selection of thread is "unspecified". So I decided to checkout the C++ specification and referred Section 30.5.1 of November 2014 working draft (available for free), it said - "Effects: If any threads are blocked waiting for *this, unblocks one of those threads.". Clearly, even the specification does not mention what the selection is based on, then I decided to put up this question. PS : Sorry for delay in response, I was travelling :)
    – AbhinavG
    Commented Jul 8, 2015 at 11:14

1 Answer 1

13

Arbitrarily. The handles of waiting threads are pushed into a container and on notify_one it will pick one, wake it and remove it. Usually the one most easily found in the structure.

If you specify that the condition is fair then it's the oldest one. Often selected by keeping an linked list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.