-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ringbuffer.cc
54 lines (52 loc) · 1.19 KB
/
test_ringbuffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <thread>
#include "sequencer.h"
#include "ringbuffer.h"
#include "order.h"
using namespace std;
int main() {
Sequencer *sequencer;
RingBuffer<Order> *ring;
Order *order;
long sequence;
// single next/modify/publish
sequencer = new Sequencer(16);
ring = new RingBuffer<Order>(sequencer);
sequence = ring->next(1);
order = ring->get(sequence);
order->id = 0;
order->from = 0;
order->to = 9;
order->seat = -1;
ring->publish(sequence);
if (sequence == ring->wait_for(sequence)) {
cout << "single next/get/publish...ok" << endl;
} else {
cout << "single next/get/publish...fail" << endl;
}
// multiple next/modify/publish
sequencer = new Sequencer(4);
ring = new RingBuffer<Order>(sequencer);
sequence = ring->next(10);
for (int i = sequence - (10 - 1); i <= sequence; i++) {
order = ring->get(sequence);
order->id = i;
order->from = 0;
order->to = 9;
order->seat = -1;
}
thread publish([ring]() {
for (int i = 0; i < 10; i++) {
ring->publish(i);
}
});
thread consume([ring]() {
for (int i = 0; i < 10; i++) {
ring->wait_for(i);
}
});
publish.join();
consume.join();
cout << "1-producer/1-consumer...ok" << endl;
return 0;
}