-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
174 lines (137 loc) · 4.16 KB
/
ConceptualExample.cpp
File metadata and controls
174 lines (137 loc) · 4.16 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// ===========================================================================
// ConceptualExample.cpp // Active Object Pattern
// ===========================================================================
// #include <iostream>
#include <functional>
#include <thread>
#include <queue>
#include <mutex>
#include "../Logger/Logger.h"
// ---------------------------------------------------------------------------
// OriginalClass is a regular class,
// that provides two methods that set a double to be a certain value.
// The two methods shouldn't be called at the same time in different threads,
// furthermore does this class NOT conform to the active object pattern.
class OriginalClass
{
private:
double m_value;
public:
OriginalClass() : m_value{} {}
void doSomething() {
Logger::log(std::cout, "doSomething");
m_value = 1.0;
}
void doSomethingElse() {
Logger::log(std::cout, "doSomethingElse");
m_value = 2.0;
}
};
// ---------------------------------------------------------------------------
// Alternate implementation using an Active Object approach
using Operation = std::function<void()>;
class DispatchQueue {
std::mutex m_mutex;
std::condition_variable m_empty;
std::queue<Operation> m_queue;
public:
void put(Operation op)
{
std::lock_guard<std::mutex> guard{ m_mutex };
m_queue.push(op);
m_empty.notify_one();
}
Operation take()
{
Operation op;
{
std::unique_lock<std::mutex> guard{ m_mutex };
m_empty.wait(guard, [&] { return !m_queue.empty(); });
op = m_queue.front();
m_queue.pop();
}
return op;
}
size_t size()
{
std::lock_guard<std::mutex> guard{ m_mutex };
return m_queue.size();
}
};
class ActiveObject
{
private:
DispatchQueue m_dispatchQueue;
std::atomic<bool> m_done;
std::unique_ptr<std::thread> m_runnable;
double m_value;
public:
// c'tors/ d'tor
ActiveObject() : m_value{}, m_done{}
{
// Start the thread that this object will "occupy".
// The public methods of this object will run in the
// context of this new thread
m_runnable = std::make_unique<std::thread>(&ActiveObject::run, this);
}
~ActiveObject() {
m_runnable->join();
}
// public interface
void run()
{
Logger::log(std::cout, "> run");
while (! m_done)
{
Operation operation = m_dispatchQueue.take();
m_done = m_dispatchQueue.size() == 0;
operation();
}
Logger::log(std::cout, "< run");
}
// This is part of the public interface of this class.
// Each method is composed of the actual code we want to execute,
// and adorn code which queues our code.
void doSomething()
{
Logger::log(std::cout, "preparing doSomething");
// Actual code to be executed is stored in a lambda
// and pushed to the 'activation list' queue
m_dispatchQueue.put({ [this]() {
Logger::log(std::cout, "doSomething");
m_value = 1.0; }
});
}
void doSomethingElse()
{
Logger::log(std::cout, "preparing doSomethingElse");
m_dispatchQueue.put( { [this] () {
Logger::log(std::cout, "doSomethingElse");
m_value = 2.0; }
});
}
};
static void test_conceptual_example_01()
{
Logger::log(std::cout, "OriginalClass Object");
OriginalClass object;
object.doSomething();
object.doSomethingElse();
Logger::log(std::cout, "Done.");
}
static void test_conceptual_example_02()
{
Logger::log(std::cout, "Active Object");
ActiveObject activeObject{};
activeObject.doSomething();
activeObject.doSomethingElse();
Logger::log(std::cout, "Done.");
}
void test_conceptual_example()
{
test_conceptual_example_01();
test_conceptual_example_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================