condition variable wait blocks thread join

62 Views Asked by At
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <iostream>

template <typename T>
class ThreadSafeQueue {
 private:
  std::queue<T> data_;
  mutable std::mutex mut_;
  std::condition_variable cv_;

 public:
  ThreadSafeQueue() {}
  ThreadSafeQueue(const ThreadSafeQueue& other) {
    std::lock_guard<std::mutex> lk(other.mut_);
    data_ = other.data_;
  }

  void push(T item) {
    std::lock_guard<std::mutex> lk(mut_);
    data_.push(std::move(item));
    cv_.notify_one();
  }

  void wait_and_pop(T& item) {
    std::unique_lock<std::mutex> ulk(mut_);
    std::cout << "Here " << std::endl;
    cv_.wait(ulk, [this] {
       return !data_.empty(); });
    item = std::move(data_.front());
    data_.pop();
  }

  bool try_pop(T& item) {
    std::lock_guard<std::mutex> lk(mut_);
    if (data_.empty()) return false;
    item = data_.front();
    data_.pop();
    return true;
  }

  bool empty() const {
    std::lock_guard<std::mutex> lk(mut_);
    return data_.empty();
  }
};

int main() {
  std::vector<int> output(3);
  std::vector<std::thread> threads;
  for (int i = 0; i < 3; i++) {
    threads.push_back(std::thread(&util::ThreadSafeQueue<int>::wait_and_pop,
                                  &ts_que, std::ref(output[i])));
    threads[i].join(); // Code is hanging here after the first thread joined.
  }
}

Trying the threadsafe queue example from currency textbook, but it seems the wait_and_pop condition_variable wait is hanging and blocking.

Was expecting the wait to be non-blocking? In the code, after first thread join, it is handing there and wait for the signal inside the wait_and_pop where the condition_variable is waiting for some data.

0

There are 0 best solutions below