Based on C++ Equivalent to Java's BlockingQueue
void push(T const& value) { // original version
{
std::unique_lock<std::mutex> lock(this->d_mutex);
d_queue.push_front(value);
}
this->d_condition.notify_one();
}
void push(T const& value) { // my question
//{ // comment out the scope
std::unique_lock<std::mutex> lock(this->d_mutex);
d_queue.push_front(value);
//} // comment out the scope
this->d_condition.notify_one();
}
Question: why we should introduce a local scope {} to cover the std::unique_lock inside the put function? Is it because we should release the lock first before calling notify_one otherwise the lock is hold while we calling the notify_one?
T pop() {
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{ return !this->d_queue.empty(); });
T rc(std::move(this->d_queue.back()));
this->d_queue.pop_back();
return rc;
}
Question: why we should use [=] inside the pop function?