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

Class ProducerConsumerQueueBenchmark

examples/spsc.cpp:76–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected