Skip to content

Commit ff04a63

Browse files
committed
refactor: [threads] Use tests.h
* Add aliases to pair of ints & vector of pair of ints.
1 parent bf40c25 commit ff04a63

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

cpp/cpp/threads-odd-even.cpp

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,83 @@
88
* thread 2: 4
99
*/
1010

11-
#include <condition_variable>
12-
#include <iostream>
13-
#include <mutex>
14-
#include <thread>
11+
#include "tests.h"
1512

13+
/* ===========================================================================
14+
* Algorithms implementation
15+
* ===========================================================================
16+
*/
17+
18+
#define _th_oe_MutexCondVar_desc "Mutex & Conditional Variable"
19+
20+
namespace MutexCondVar
21+
{
1622
/* threads synchronization primitives */
17-
std::mutex mx;
18-
std::condition_variable cv;
23+
mutex mx;
24+
condition_variable cv;
1925

2026
/* shared data between threads */
2127
static int sd = 1;
2228

2329
#define MAX_DATA 10
2430

2531
/* cv.wait() predicate objects don't take args */
26-
static inline bool is_odd() { return sd % 2; }
27-
static inline bool is_even() { return !is_odd(); }
32+
static bool is_odd() { return sd % 2; }
33+
static bool is_even() { return !is_odd(); }
2834

29-
void print_odd(int tidx)
35+
using Predicate = function<bool()>;
36+
vpi_t a;
37+
38+
void print(int tidx, Predicate pred)
3039
{
3140
while (sd < MAX_DATA) {
32-
std::unique_lock<std::mutex> lk(mx);
41+
unique_lock<mutex> lk(mx);
3342
/* cv.wait(lock, predicate) handles spurious wake up */
34-
cv.wait(lk, is_even);
35-
if (getenv("SHOW_TEST_OUTPUT"))
36-
std::cout << " thread " << tidx << ": " << sd
37-
<< "\n";
43+
cv.wait(lk, pred);
44+
a.emplace_back(tidx, sd);
3845
sd++;
3946
cv.notify_one();
4047
}
4148
}
49+
} // namespace MutexCondVar
50+
51+
// TODO namespace Semaphores
4252

43-
void print_even(int tidx)
53+
/* ===========================================================================
54+
* Test code
55+
* ===========================================================================
56+
*/
57+
ostream &operator<<(ostream &out, const vpi_t &c)
4458
{
45-
while (sd < MAX_DATA) {
46-
std::unique_lock<std::mutex> lk(mx);
47-
/* cv.wait(lock, predicate) handles spurious wake up */
48-
cv.wait(lk, is_odd);
49-
if (getenv("SHOW_TEST_OUTPUT"))
50-
std::cout << " thread " << tidx << ": " << sd
51-
<< "\n";
52-
sd++;
53-
cv.notify_one();
59+
int n = size(c);
60+
fii (i, n) {
61+
int _tidx = c[i].first, _sd = c[i].second;
62+
out << format("\n thread {}: {}", _tidx, _sd);
5463
}
64+
return out;
5565
}
5666

57-
int main(int, char **)
58-
{
59-
int tidx[2] = {0, 1};
67+
#define _th_oe_desc_prefix "Threads odd even"
6068

61-
if (getenv("SHOW_TEST_OUTPUT"))
62-
std::cout << "Testing implementation " << 1 << " "
63-
<< "sync 2 threads to print odd & even numbers"
64-
<< "\n";
69+
#define _TH_OE_NAME(var) var
70+
#define _TH_OE_DESC(var) _th_oe_desc_prefix " - " _th_oe_##var##_desc
6571

66-
std::thread t1(print_odd, tidx[0]);
67-
std::thread t2(print_even, tidx[1]);
68-
t1.join();
69-
t2.join();
72+
#define _TH_OE_TEST(var) \
73+
TEST(_TH_OE_NAME(var), _TH_OE_DESC(var)) \
74+
{ \
75+
using namespace _TH_OE_NAME(var); \
76+
vpi_t e; \
77+
int n = MAX_DATA; \
78+
fii (i, n) e.emplace_back(i % 2, i + 1); \
79+
int t = 0; \
80+
vector<thread> th; \
81+
th.emplace_back(move(thread(print, t++, is_odd))); \
82+
th.emplace_back(move(thread(print, t++, is_even))); \
83+
for (auto &x : th) x.join(); \
84+
CHECK_EQ(e, a); \
85+
SHOW_OUTPUT(n, a); \
86+
}
7087

71-
std::cout << "Executed " << 1 << " implementations"
72-
<< " with " << 1 << " tests." << std::endl;
73-
return 0;
74-
}
88+
_TH_OE_TEST(MutexCondVar);
89+
90+
INIT_TEST_MAIN();

cpp/includes/tests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ static vector<_test *> _tests;
149149
#define vi_t vector<int>
150150
#define vi2_t vector<vi_t>
151151
#define umi_t unordered_map<int, int>
152+
#define pii_t pair<int, int>
153+
#define vpi_t vector<pii_t>
152154

153155
#define vi_v(v, n, i) vi_t v((n), (i))
154156
#define vi2_v(v, n, i) vi2_t v((n), vi_t((n), (i)))

0 commit comments

Comments
 (0)