Skip to content

Commit

Permalink
[09_queue] concurrency, block_queue, wait_for, done.
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam0205 committed Oct 11, 2018
1 parent 1289a1c commit 82b0b47
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions c-cpp/09_queue/block_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ class BlockQueue {
container_.pop();
not_full_.notify_one();
}
template <typename Duration>
bool put_for(const value_type& item, const Duration& d) {
std::unqiue_lock<std::mutex> lock(mutex_);
if (not_full_.wait_for(lock, d, [&](){ return not full(); })) {
container_.push(item);
not_empty_.notify_one();
return true;
} else {
return false;
}
}
template <typename Duration>
bool take_for(const Duration& d, value_type& out) {
std::unique_lock<std::mutex> lock(mutex_);
if (not_empty_.wait_for(lock, d, [&](){ return not empty(); })) {
out = container_.front();
container_.pop();
not_full_.notify_one();
return true;
} else {
return false;
}
}
};

#endif // QUEUE_BLOCK_QUEUE_HPP_
Expand Down

0 comments on commit 82b0b47

Please sign in to comment.