-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadingExperiments.cpp
More file actions
396 lines (335 loc) · 11.8 KB
/
ThreadingExperiments.cpp
File metadata and controls
396 lines (335 loc) · 11.8 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "pch.h"
#include <thread>
#include <sstream>
#include <time.h>
#include <ctime>
#include <string>
#include "PrioritizedStagedProcessor.h"
//#include "Scheduler.h"
//#include "ItemProcessor.h"
#include "ProcessorGroup.h"
#include "NamedItemStore.h"
#include "Scheduler.h"
void TestPrioritizedStagedProcessing() {
std::vector<ndtech::PrioritizedProcessingStage<std::string>> stages{
ndtech::PrioritizedProcessingStage(
"Stage1",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage1 Processing: item = " << item << std::endl;
return item;
})),
ndtech::PrioritizedProcessingStage(
"Stage2",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage2 Processing: item = " << item << std::endl;
return item;
})),
ndtech::PrioritizedProcessingStage(
"Stage3",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage3 Processing: item = " << item << std::endl;
return item;
}))
};
stages[0].m_items.push_back("test0.0");
stages[0].m_items.push_back("test0.1");
stages[1].m_items.push_back("test1.0");
stages[1].m_items.push_back("test1.1");
auto processing = ndtech::CreatePrioritizedStagedProcessor(stages);
std::cout << "PrioritizedStagedProcessing created" << std::endl;
processing.Start();
std::string line;
while (std::getline(std::cin, line))
{
if (line == "q") {
std::cout << "Received quit signal" << std::endl;
break;
}
if (line == "a") {
std::cout << "Adding Item: Enter Item Name: ";
std::getline(std::cin, line);
std::cout << "Adding Item: " << line << std::endl;
processing.AddItem(line);
}
else {
std::cout << "Read '" << line << "' from stdin" << std::endl;
}
}
std::cout << "Exited UI loop" << std::endl;
processing.Join();
std::cout << "Processing thread joined. UI thread preparing to exit" << std::endl;
}
void TestProcessorGroup() {
std::vector<ndtech::PrioritizedProcessingStage<std::string>> stages;
stages.emplace_back("PrioritizedProcessingStage1:Stage1",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage1.1 Processing: item = " << item << std::endl;
std::cout << "Stage1.1 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages.emplace_back("PrioritizedProcessingStage1:Stage2",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage1.2 Processing: item = " << item << std::endl;
std::cout << "Stage1.2 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages.emplace_back(
"PrioritizedProcessingStage1:Stage3",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage1.3 Processing: item = " << item << std::endl;
std::cout << "Stage1.3 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages[0].m_items.emplace_back("test0.0");
stages[0].m_items.emplace_back("test0.1");
stages[1].m_items.emplace_back("test1.0");
stages[1].m_items.emplace_back("test1.1");
auto processing = ndtech::CreatePrioritizedStagedProcessor(stages);
std::cout << "PrioritizedStagedProcessing created" << std::endl;
std::vector<ndtech::PrioritizedProcessingStage<std::string>> stages2;
stages2.emplace_back("PrioritizedProcessingStage2:Stage1",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage2.1 Processing: item = " << item << std::endl;
std::cout << "Stage2.1 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages2.emplace_back("PrioritizedProcessingStage2:Stage2",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage2.2 Processing: item = " << item << std::endl;
std::cout << "Stage2.2 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages2.emplace_back(
"PrioritizedProcessingStage2:Stage3",
std::function<std::string(std::string)>([](std::string item) {
std::cout << "Stage2.3 Processing: item = " << item << std::endl;
std::cout << "Stage2.3 Processing: threadId = " << std::this_thread::get_id() << std::endl;
return item;
}));
stages2[0].m_items.emplace_back("test2.0.0");
stages2[0].m_items.emplace_back("test2.0.1");
stages2[1].m_items.emplace_back("test2.1.0");
stages2[1].m_items.emplace_back("test2.1.1");
auto processing2 = ndtech::CreatePrioritizedStagedProcessor(stages2);
std::cout << "PrioritizedStagedProcessing2 created" << std::endl;
ndtech::ProcessorGroup processorGroup(processing, processing2);
processorGroup.Start();
std::string line;
while (std::getline(std::cin, line))
{
if (line == "q") {
std::cout << "Received quit signal" << std::endl;
break;
}
if (line == "a") {
std::cout << "Adding Item: Enter Item Name: ";
std::getline(std::cin, line);
std::cout << "Adding Item: " << line << std::endl;
processing.AddItem(line);
}
else {
std::cout << "Read '" << line << "' from stdin" << std::endl;
}
}
std::cout << "Exited UI loop" << std::endl;
processorGroup.Join();
std::cout << "Processing thread joined. UI thread preparing to exit" << std::endl;
}
void TestNamedItemStore() {
ndtech::NamedItemStore<std::string> store(
[](std::pair<std::string, std::string> item) {
std::cout << "Processing namedItem : " << item.first << std::endl;
std::this_thread::sleep_for(2s);
return item;
});
store.AddItem("item1", "i1");
store.AddItem("item2", "i2");
store.Start();
std::string line;
while (std::getline(std::cin, line))
{
if (line == "q") {
std::cout << "Received quit signal" << std::endl;
break;
}
if (line == "a") {
std::cout << "Adding Item: Enter Item Name: ";
std::string newName;
std::getline(std::cin, newName);
std::cout << "Adding Item: Enter Item value: ";
std::string newValue;
std::getline(std::cin, newValue);
std::cout << "Adding Item: NAME = " << newName << " VALUE = " << newValue << std::endl;
store.AddItem(newName, newValue);
}
if (line == "g") {
std::cout << "Getting Item: Enter Item Name: ";
std::string itemName;
std::getline(std::cin, itemName);
std::cout << "Getting Item: NAME = " << itemName << std::endl;
std::string str = store.GetItem(itemName);
std::cout << "Got Item: NAME = " << itemName << " VALUE = " << str << std::endl;
}
else {
std::cout << "Read '" << line << "' from stdin" << std::endl;
}
}
std::cout << "Exited UI loop" << std::endl;
std::string item1Result = store.GetItem("item1");
std::cout << "Got item1" << std::endl;
store.Join();
std::cout << "Processing thread joined. UI thread preparing to exit" << std::endl;
}
void InitializeNamedItemStore(ndtech::NamedItemStore<std::string>* store) {
store->AddItem("item1", "i1");
store->AddItem("item2", "i2");
store->Start();
}
template <typename ItemType>
void AddItemToStore(ndtech::NamedItemStore<ItemType>* store, std::string name, ItemType item) {
std::thread thread = std::thread{ [store, name, item]() { store->AddItem(name, item); } };
thread.join();
}
int main()
{
// This is the ui thread
// all background work must happen on background tasks
std::cout << "main threadId = " << std::this_thread::get_id() << std::endl;
ndtech::Scheduler m_scheduler;
m_scheduler.AddTask(
std::pair<std::function<void(void)>, time_point<system_clock>>{
[]() {
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << "Inside Task2: time is " << std::ctime(&now_c) << std::endl;
std::cout << ss.str().c_str();
//DebugOutput(ss.str().c_str());
},
system_clock::now() + 3s
});
m_scheduler.AddTask(
std::pair<std::function<void(void)>, time_point<system_clock>>{
[]() {
std::stringstream ss;
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
ss << "Inside Task1: time is " << std::ctime(&now_c) << std::endl;
std::cout << ss.str().c_str();
//DebugOutput(ss.str().c_str());
},
system_clock::now()
});
m_scheduler.AddTask(
std::pair<std::function<void(void)>, time_point<system_clock>>{
[]() {
std::stringstream ss;
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
ss << "Inside Task3: time is " << std::ctime(&now_c) << std::endl;
std::cout << ss.str().c_str();
//DebugOutput(ss.str().c_str());
},
system_clock::now() + 1s
});
m_scheduler.AddRepeatingTask(
[]() {
std::stringstream ss;
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
ss << "Inside RepeatingTask1: time is " << std::ctime(&now_c) << std::endl;
std::cout << ss.str().c_str();
//DebugOutput(ss.str().c_str());
},
1000ms,
system_clock::now() + 5s
);
m_scheduler.AddRepeatingTask(
[]() {
std::stringstream ss;
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
ss << "Inside RepeatingTask2: time is " << std::ctime(&now_c) << std::endl;
std::cout << ss.str().c_str();
//DebugOutput(ss.str().c_str());
},
2000ms,
system_clock::now() + 10s
);
//TestPrioritizedStagedProcessing();
//TestProcessorGroup();
ndtech::NamedItemStore<std::string> store(
[](std::pair<std::string, std::string> item) {
std::cout << "Processing namedItem : " << item.first << std::endl;
std::this_thread::sleep_for(2s);
return item;
});
InitializeNamedItemStore(&store);
std::string line;
while (std::getline(std::cin, line))
{
if (line == "q") {
std::cout << "Received quit signal" << std::endl;
break;
}
if (line == "a") {
std::cout << "Adding Item: Enter Item Name: ";
std::string newName;
std::getline(std::cin, newName);
std::cout << "Adding Item: Enter Item value: ";
std::string newValue;
std::getline(std::cin, newValue);
std::cout << "Adding Item: NAME = " << newName << " VALUE = " << newValue << std::endl;
store.AddItem(newName, newValue);
//AddItemToStore(&store, newName, newValue);
}
if (line == "g") {
std::cout << "Getting Item: Enter Item Name: ";
std::string itemName;
std::getline(std::cin, itemName);
std::cout << "Getting Item: NAME = " << itemName << std::endl;
std::string str = store.GetItem(itemName);
std::cout << "Got Item: NAME = " << itemName << " VALUE = " << str << std::endl;
}
else {
std::cout << "Read '" << line << "' from stdin" << std::endl;
}
}
std::cout << "Exited UI loop" << std::endl;
std::string item1Result = store.GetItem("item1");
std::cout << "Got item1" << std::endl;
m_scheduler.Join();
store.Join();
std::cout << "Processing thread joined. UI thread preparing to exit" << std::endl;
return 0;
}
//#include <string>
//#define NONIUS_RUNNER
//#include <nonius/nonius.h++>
//#include <nonius/main.h++>
//#include <iostream>
//
//using namespace std;
//
//string short_string = "hello";
//string long_string("0123456789abcdefghijklmnopqrstuvwxyz");
//
//void Test1() {
// std::cout << "Test1";
//}
//
////NONIUS_BENCHMARK("StringCopyShort", []
//// {
//// string copy(short_string);
//// })
////
//// NONIUS_BENCHMARK("StringCopyLong", []
//// {
//// string copy(long_string);
//// })
//
//
//NONIUS_BENCHMARK("Test1", []
// {
// Test1();
// })