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

Class MPMCBoundedQueueBenchmark

examples/mpmc.cpp:77–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75};
76
77class MPMCBoundedQueueBenchmark : public CppBenchmark::BenchmarkPC
78{
79public:
80 using BenchmarkPC::BenchmarkPC;
81
82protected:
83 void Initialize(CppBenchmark::ContextPC& context) override
84 {
85 _queue = std::make_shared<mpmc_bounded_queue_t<int>>(queue_bound_size);
86 _count = 1;
87 }
88
89 void Cleanup(CppBenchmark::ContextPC& context) override
90 {
91 _queue.reset();
92 }
93
94 void RunProducer(CppBenchmark::ContextPC& context) override
95 {
96 if (_count >= items_to_produce)
97 {
98 int value = 0;
99 if (_queue->enqueue(value))
100 context.StopProduce();
101 return;
102 }
103
104 int value = _count;
105 if (_queue->enqueue(value))
106 ++_count;
107 }
108
109 void RunConsumer(CppBenchmark::ContextPC& context) override
110 {
111 int value = -1;
112 if (_queue->dequeue(value) && (value == 0))
113 context.StopConsume();
114 }
115
116private:
117 std::shared_ptr<mpmc_bounded_queue_t<int>> _queue;
118 std::atomic<int> _count;
119};
120
121class LockBoundedQueueBenchmark : public CppBenchmark::BenchmarkPC
122{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected