Concurrent Queue Lock in C ++ 11

To transfer messages between threads, I am looking for a parallel queue with the following properties:

  • limited size
  • A pop method that locks / waits until an item is available.
  • abort method to cancel waiting
  • Optional: priority

Several manufacturers, one consumer.

concurrent_bounded_queue TBB will provide this, but I am looking for alternatives to avoid the additional TBB dependency.

The application uses C ++ 11 and boost. I could not find anything suitable in boxing. What are the options?

+4
source share
1 answer

A naive implementation using the Boost library (round_buffer) and the C ++ 11 standard library.

 #include <mutex> #include <condition_variable> #include <boost/circular_buffer.hpp> struct operation_aborted {}; template <class T, std::size_t N> class bound_queue { public: typedef T value_type; bound_queue() : q_(N), aborted_(false) {} void push(value_type data) { std::unique_lock<std::mutex> lk(mtx_); cv_pop_.wait(lk, [=]{ return !q_.full() || aborted_; }); if (aborted_) throw operation_aborted(); q_.push_back(data); cv_push_.notify_one(); } value_type pop() { std::unique_lock<std::mutex> lk(mtx_); cv_push_.wait(lk, [=]{ return !q_.empty() || aborted_; }); if (aborted_) throw operation_aborted(); value_type result = q_.front(); q_.pop_front(); cv_pop_.notify_one(); return result; } void abort() { std::lock_guard<std::mutex> lk(mtx_); aborted_ = true; cv_pop_.notify_all(); cv_push_.notify_all(); } private: boost::circular_buffer<value_type> q_; bool aborted_; std::mutex mtx_; std::condition_variable cv_push_; std::condition_variable cv_pop_; }; 
+5
source

Source: https://habr.com/ru/post/1493400/


All Articles