Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88a4384

Browse files
committed
Added an example
1 parent 8e83a2d commit 88a4384

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,63 @@ $ cmake ..
2828
$ make
2929
$ ./disruptor4cpp_test
3030
```
31+
32+
To use it, include the below header file
33+
```cpp
34+
#include <disruptor4cpp/disruptor4cpp.h>
35+
```
36+
37+
## Example
38+
```cpp
39+
#include <cstdint>
40+
#include <exception>
41+
#include <iostream>
42+
#include <thread>
43+
44+
#include <disruptor4cpp/disruptor4cpp.h>
45+
46+
class int_handler : public disruptor4cpp::event_handler<int>
47+
{
48+
public:
49+
int_handler() = default;
50+
virtual ~int_handler() = default;
51+
virtual void on_start() { }
52+
virtual void on_shutdown() { }
53+
virtual void on_event(int& event, int64_t sequence, bool end_of_batch)
54+
{
55+
std::cout << "Received integer: " << event << std::endl;
56+
}
57+
virtual void on_timeout(int64_t sequence) { }
58+
virtual void on_event_exception(const std::exception& ex, int64_t sequence, int* event) { }
59+
virtual void on_start_exception(const std::exception& ex) { }
60+
virtual void on_shutdown_exception(const std::exception& ex) { }
61+
};
62+
63+
int main(int argc, char* argv[])
64+
{
65+
using namespace disruptor4cpp;
66+
67+
// Create the ring buffer.
68+
ring_buffer<int, 1024, busy_spin_wait_strategy, producer_type::multi> ring_buffer;
69+
70+
// Create and run the consumer on another thread.
71+
auto barrier = ring_buffer.new_barrier();
72+
int_handler handler;
73+
batch_event_processor<decltype(ring_buffer)> processor(ring_buffer, std::move(barrier), handler);
74+
std::thread processor_thread([&processor] { processor.run(); });
75+
76+
// Publish some integers.
77+
for (int i = 0; i < 1000; i++)
78+
{
79+
int64_t seq = ring_buffer.next();
80+
ring_buffer[seq] = i;
81+
ring_buffer.publish(seq);
82+
}
83+
84+
// Stop the consumer.
85+
std::this_thread::sleep_for(std::chrono::seconds(1));
86+
processor.halt();
87+
processor_thread.join();
88+
return 0;
89+
}
90+
```

0 commit comments

Comments
 (0)