played with thread syncing - #10
Conversation
maksimpustovoyt
left a comment
There was a problem hiding this comment.
Please address 1 comment and merge
| Q(size_t cap) : _cap(cap) {} | ||
|
|
||
| void add(T x) { | ||
| unique_lock<mutex> lock(m); |
maksimpustovoyt
left a comment
There was a problem hiding this comment.
Please address 1 comment and merge
Let's discuss today the strategy
| public: | ||
| Q(size_t cap) : _cap(cap) {} | ||
|
|
||
| void add(T x) { |
There was a problem hiding this comment.
Let's discuss T x, Can it be const T & ? or can it be std::move()
|
Maksim, I think this implementation is wrong: If add() is called when the queue already has space (queue.size() < _cap), no other thread is required to call not_full.notify_one(). Since condition variables do not queue notifications, this thread can go to sleep with space available and never wake up → potential infinite wait. Also, according to cppreference.com, any thread that intends to wait on a std::condition_variable must:
|
| // } | ||
| // } | ||
|
|
||
| while (queue.size() >= _cap) { |
There was a problem hiding this comment.
We already discuss it... the code has race for queue.size()
| // } | ||
| // } | ||
|
|
||
| while (queue.empty()) { |
There was a problem hiding this comment.
We already discuss it... the code has race for queue.empty()
| while (queue.size() >= _cap) { | ||
| if (not_full.wait_for(lk, timeout) == cv_status::timeout) | ||
| return false; | ||
| } |
There was a problem hiding this comment.
We already discuss it... the code has race for queue.size()
| while (queue.empty()) { | ||
| if (not_empty.wait_for(lk, timeout) == cv_status::timeout) | ||
| return false; | ||
| } |
There was a problem hiding this comment.
We already discuss it... the code has race for queue.empty()
No description provided.