#pragma once #include #include namespace bts { namespace utils { // Wraps a bounded single producer, single consumer to provide extra // dynamically allocated storage when the wrapped queue is full. The // assumption is that filling the queue is a very rare event for which // we can afford some performance penalty. Producers must periodically // call drain() to ensure that events bufffered while the underlying // queue was full are eventually put back in the underlying queue template class GrowableSPSCQueue { public: GrowableSPSCQueue(size_t internal_size) : _queue(internal_size), _overflow_buffer() {} // producer void drain() { while (_overflow_buffer.size()) { auto const &t = _overflow_buffer.front(); if (_queue.push(t)) _overflow_buffer.pop(); else return; } } void push(const T &t) { if (_overflow_buffer.size() || !_queue.push(t)) _overflow_buffer.push(t); } // consumer bool pop(T &t) { return _queue.pop(t); } template void consume_all(C const&c) { _queue.consume_all(c); } private: boost::lockfree::spsc_queue _queue; std::queue _overflow_buffer; }; } // namespace utils } // namespace bts