0

Let's say I have two groups of threads. One group's function is to add an element to an array, and the other group's function is to remove an element from the array if the array contains the same element. The rule is that thread can't remove an element from the array if it's empty and it must wait. A monitor is used to solve this synchronization problem.

Consider a scenario in which all threads start at the same time, the consumer thread locks the mutex first and then it checks if the array is not empty, the condition is false, so it unlocks the mutex. Then the producer thread locks the mutex first, adds an elements and notifies all the waiting threads and unlocks the mutex. The question is, does the waiting thread gets the access to the mutex first after it was notified and the waiting thread can try to remove the element or the mutex is free again and any thread can lock it by chance again and the waiting thread is not finished after the condition fail, but let's say it's put back into the thread pool.

1
  • 2
    You better show some (pseudo) code, it is very difficult to understand what you are asking. Commented Oct 19, 2017 at 19:30

1 Answer 1

2

First, lets make some things clear (Includes an essence of cppreference's page about std::condition_variable):

Consumers

Consumers wait on std::condition_variable-s using the following steps:

  • Acquire a std::unique_lock<std::mutex>, on the same mutex as used to protect the shared variable
  • Execute wait, wait_for, or wait_until. The wait operations atomically release the mutex and suspend the execution of the thread.
  • When the condition variable is notified, a timeout expires, or a spurious wakeup occurs, the thread is awakened, and the mutex is atomically reacquired. The thread should then check the condition and resume waiting if the wake up was spurious.

Producers

Producers notify the same std::condition_variable-s following these steps:

  • Acquire a std::mutex (typically via std::lock_guard)
  • Perform the modification while the lock is held
  • Execute notify_one or notify_all on the std::condition_variable (the lock does not need to be held for notification)

Answer

does the waiting thread gets the access to the mutex first after it was notified and the waiting thread can try to remove the element

Yes, there may be multiple consumers on the same condition, and each and every one of them may consume the single object, this is why every waiting thread should protect against spurious wake-ups using an additional logical condition (See the bold text in the consumer part). The wait methods of std::condition_variable even has a specific prototype that already includes it:

template< class Predicate >
void wait( std::unique_lock<std::mutex>& lock, Predicate pred );

When the consumer wakes up, it has already acquired the lock! So, if the condition (for example !queue->empty()) is fulfilled, it can safely consume.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.