MCPcopy Create free account
hub / github.com/chronoxor/CppBenchmark / ReaderWriterQueueBenchmark

Class ReaderWriterQueueBenchmark

examples/spsc.cpp:34–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32
33template <class TReaderWriterQueue>
34class ReaderWriterQueueBenchmark : public CppBenchmark::BenchmarkPC
35{
36public:
37 using BenchmarkPC::BenchmarkPC;
38
39protected:
40 void Initialize(CppBenchmark::ContextPC& context) override
41 {
42 _queue = std::make_shared<TReaderWriterQueue>();
43 _count = 1;
44 }
45
46 void Cleanup(CppBenchmark::ContextPC& context) override
47 {
48 _queue.reset();
49 }
50
51 void RunProducer(CppBenchmark::ContextPC& context) override
52 {
53 if (_count >= items_to_produce)
54 {
55 if (_queue->try_enqueue(0))
56 context.StopProduce();
57 return;
58 }
59
60 if (_queue->try_enqueue(_count))
61 ++_count;
62 }
63
64 void RunConsumer(CppBenchmark::ContextPC& context) override
65 {
66 int value = -1;
67 if (_queue->try_dequeue(value) && (value == 0))
68 context.StopConsume();
69 }
70
71private:
72 std::shared_ptr<TReaderWriterQueue> _queue;
73 std::atomic<int> _count;
74};
75
76class ProducerConsumerQueueBenchmark : public CppBenchmark::BenchmarkPC
77{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected