template<typename T, class BasicLockable = std::mutex>
class rangeless::mt::synchronized_queue< T, BasicLockable >
Optionally-bounded blocking concurrent MPMC queue.
- Supports not-copy-constructible/not-default-constructible value-types (just requires move-assigneable).
- Can be used with lockables other than
std::mutex
, e.g. mt::atomic_lock
.
- Contention-resistant: when used with
mt::atomic_lock
the throughput is comparable to state-of-the-art lock-free implementations.
- Short and simple implementation using only c++11 standard library primitives.
- Provides RAII-based closing semantics to communicate end-of-inputs from the pushing end or failure/going-out-of-scope from the popping end.
Related:
boost::fibers::buffered_channel
boost::sync_bounded_queue
boost::lockfree::queue
tbb::concurrent_queue
moodycamel::BlockingConcurrentQueue
using queue_t = mt::synchronized_queue<std::future<size_t> >;
queue_t queue{ 10 };
auto fut = std::async(std::launch::async,[ &queue ]
{
auto close_on_exit = queue.close();
for(std::string line; std::getline(std::cin, line); ) {
queue <<=
std::async(
std::launch::async,
[](const std::string& s) {
return s.size();
},
std::move(line));
}
});
size_t total = 0;
queue >>= [&](queue_t::value_type x) { total += x.get(); };
fut.get();
Definition at line 6413 of file fn.hpp.
template<typename T, class BasicLockable = std::mutex>
Return an RAII object that will close the queue in its destructor.
auto guard = queue.close();
queue.close();
NB: closing is non-blocking.
Blocked calls to try_push()/try_pop() shall return with status::closed.
Blocked calls to push()/pop() shall throw queue_closed
.
Subsequent calls to push()/try_push() shall do as above.
Subsequent calls to pop()/try_pop() will succeed until the queue becomes empty, and throw/return-closed thereafter.
Definition at line 6742 of file fn.hpp.
template<typename T, class BasicLockable = std::mutex>
template<typename F >
pop() the values into the provided sink-function until closed and empty.
e.g. queue >>= [&out_it](T x){ *out_it++ = std::move(x); };
Queue is automatically closed if exiting via exception, unblocking the pushers.
Definition at line 6553 of file fn.hpp.