-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pipeline.cpp
79 lines (64 loc) · 1.4 KB
/
Pipeline.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Pipeline.h"
#include "Executor.h"
#include "Task.h"
using namespace std;
// Pipeline::Pipeline() :
// empty( true ) {
// }
// void Pipeline::write( double value ) {
// unique_lock<mutex> lock(mtx);
// while (!empty) {
// cv.wait(lock);
// }
// empty = false;
// this->value = value;
// cv.notify_all();
// }
// double Pipeline::read() {
// unique_lock<mutex> lock(mtx);
// while (empty) {
// cv.wait(lock);
// }
// empty = true;
// cv.notify_all();
// return value;
// }
class WriteTask : public Task {
public:
Pipeline<double>& pipeline;
void execute() {
for (int i=0; i<100; i++) {
pipeline.write(i);
}
}
WriteTask( Pipeline<double>& pipeline ) :
pipeline( pipeline ) {
}
};
class ReadTask : public Task {
public:
Pipeline<double>& pipeline;
double total;
void execute() {
for (int i=0; i<100; i++) {
total+=pipeline.read();
}
}
ReadTask( Pipeline<double>& pipeline ) :
pipeline( pipeline ),
total(0.0 ) {
}
};
static void testTwoThreads() {
Pipeline<double> pipeline;
auto w = make_shared<WriteTask>( pipeline );
auto r = make_shared<ReadTask>( pipeline );
SPExecutor executor = Executor::newInstance(2);
executor->addTask( r );
executor->addTask( w );
executor->join();
ASSERT_APPROX_EQUAL( r->total, 99.0*50.0, 0.1);
}
void testPipeline() {
TEST( testTwoThreads );
}