-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ping-pong.cpp
48 lines (39 loc) · 963 Bytes
/
test_ping-pong.cpp
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
#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <doctest.h>
#include <rezsem/rezsemaphores.hpp>
std::atomic<uint64_t> ai;
rezsem::Semaphore pingSem(0, 1);
rezsem::Semaphore pongSem(1, 1);
std::vector<std::string> pingPongs;
void ping(int iters) {
for (int i = 0; i < iters; i++) {
pingSem.Acquire();
pingPongs.push_back("ping");
pongSem.Release();
}
}
void pong(int iters) {
for (int i = 0; i < iters; i++) {
pongSem.Acquire();
pingPongs.push_back("pong");
pingSem.Release();
}
}
TEST_CASE("Ping Pong") {
int iters = 50;
std::thread pt(ping, iters);
std::thread pp(pong, iters);
pt.join();
pp.join();
CHECK(pingPongs.size() == (iters * 2));
for (int i = 0; i < (iters * 2); i++) {
if (i % 2 == 0) {
CHECK(pingPongs[i] == "ping");
} else {
CHECK(pingPongs[i] == "pong");
}
}
}