-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadsWaitNotifyManager.h
More file actions
483 lines (398 loc) · 9.55 KB
/
ThreadsWaitNotifyManager.h
File metadata and controls
483 lines (398 loc) · 9.55 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
/*
项目中多个线程之间有互相等待执行的依赖关系,但是由于有些线程是用的其他库接口中的功能创建,有些是我们自己开发创建。
若线程互相依赖,原先的解决办法就是循环等待条件,间隔很短的sleep线程,然后继续检查条件是否满足。这种方式对于资源使用效率不高。
现在打算使用条件变量,使得这些线程都集中到一起进行通信,将会提高资源使用效率。
2019-08-14 新增了事件驱动的事件分发中心类,相当于事件总线,但是我不喜欢事件总线这个说法,因为技术专业术语太强。 实际上从应用角度来说,就是事件的信息分发,就如同邮局,物流等,通俗易懂。
*/
#include <thread>
#include <queue>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include "./chronoStudy.h"
using namespace std;
enum EVENT_TYPE
{
EVENT_NONE,
EVENT_TICK,
EVENT_ORDER,
EVENT_TRADE
};
struct TICK
{
char name[10];
double price;
int volumn;
};
struct ORDER
{
char name[10];
double price;
int volumn;
char SysId[10];
};
struct TRADE
{
char name[10];
double price;
int volumn;
};
union EVENT_DATA
{
TRADE trade;
ORDER order;
TICK tick;
};
struct EVENT1
{
EVENT_TYPE type;
EVENT_DATA data;
};
template<typename T>
class messenger
{
public:
T wait()
{
T data;
unique_lock<mutex> lk(mut);
//cout << getSystemClock_microSeconds() << "before wait" << endl;
data_cond.wait(lk, [this] {return !data_queue.empty(); });
//cout << getSystemClock_microSeconds()<<"after wait" << endl;
if (!data_queue.empty())
{
//cout << getSystemClock_microSeconds() << "get a message :" << (data = data_queue.back()).c_str() << endl;;
data = data_queue.front();
data_queue.pop();
}
lk.unlock();
return data;
}
void notify(T &message)
{
lock_guard<mutex> lk(mut);
data_queue.push(message);
//cout << getSystemClock_microSeconds() << "send a message"<< message.c_str() << endl;
data_cond.notify_one();
//cout << getSystemClock_microSeconds() << "notify_one" << endl;
}
void init()
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
cout << getSystemClock_microSeconds() << "init" << endl;
}
int getDepth()
{
return data_queue.size();
}
private:
mutex mut;
queue<T> data_queue;
condition_variable data_cond;
};
class Message
{
private:
static messenger<EVENT1> * mes;
public:
static void Set(messenger<EVENT1> * s)
{
mes = s;
}
static void AddEvent(EVENT1 ev1)
{
mes->notify(ev1);
}
static EVENT1 GetEvent(EVENT1)
{
return mes->wait();
}
};
void testUnion()
{
TICK t;
strcpy(t.name, "ax1");
t.price = 123;
t.volumn = 1;
EVENT1 ev1;
ev1.type = EVENT_TYPE::EVENT_TICK;
ev1.data.tick = t;
queue<EVENT1> q;
q.push(ev1);
cout << ev1.type << " " << ev1.data.tick.name << " " << ev1.data.tick.price << " " << ev1.data.tick.volumn << endl;
EVENT1 &ev2 = q.front();
q.pop();
EVENT1 ev3;
ev3.type = ev2.type;
ev3.data.tick = ev2.data.tick;
cout << ev3.type << " " << ev3.data.tick.name << " " << ev3.data.tick.price << " " << ev3.data.tick.volumn << endl;
}
class commander
{
int index_1 = 0;
int index_2 = 0;
public:
void data_preparation_thread()
{
while (index_1<10000) {
index_1 += 1;
//cout << getSystemClock_microSeconds() << __FUNCTION__ << "begin,"<< index_1 << endl;
EVENT1 ev1;
if (index_1 % 2 == 0)
{
TICK t;
strcpy(t.name, "ax1");
t.price = 1 + index_1;
t.volumn = index_1;
ev1.type = EVENT_TYPE::EVENT_TICK;
ev1.data.tick = t;
}
else
{
ORDER t;
strcpy(t.name, "or1");
t.price = 1 + index_1;
t.volumn = index_1;
ev1.type = EVENT_TYPE::EVENT_ORDER;
ev1.data.order = t;
}
mes->notify(ev1);
//cout << getSystemClock_microSeconds() << __FUNCTION__ << "end" << endl;
}
cout << mes->getDepth() << endl;
}
void data_processing_thread()
{
while (true)
{
index_2 += 1;
//cout << getSystemClock_microSeconds() << __FUNCTION__ << "begin,"<< index_2 << endl;
EVENT1 result = mes->wait();
if (result.type != EVENT_TYPE::EVENT_NONE)
{
if (result.type == EVENT_TYPE::EVENT_TICK)
cout << getSystemClock_microSeconds() << __FUNCTION__ << "result " << result.type << " " << result.data.tick.name << " " << result.data.tick.volumn << endl;
else if (result.type == EVENT_TYPE::EVENT_ORDER)
cout << getSystemClock_microSeconds() << __FUNCTION__ << "result " << result.type << " " << result.data.tick.name << " " << result.data.order.volumn << endl;
}
//cout << getSystemClock_microSeconds() << __FUNCTION__ << "end" << endl;
}
}
void init()
{
}
void setMessenger(messenger<EVENT1> * m)
{
mes = m;
}
commander(string str)
{
name = str;
}
private:
string name;
messenger<EVENT1> * mes;
};
void messengerTest()
{
messenger<EVENT1> *mes = new messenger<EVENT1>();
mes->init();
commander com("com1");
com.setMessenger(mes);
thread *t0 = nullptr;
thread *t1 = nullptr;
thread *t2 = nullptr;
t0 = new thread(&commander::data_preparation_thread, com);
t2 = new thread(&commander::data_preparation_thread, com);
t0->detach();
t2->detach();
t1 = new thread(&commander::data_processing_thread, com);
t1->join();
cout << "";
}
class IEventListener
{
public:
IEventListener() {};
virtual ~IEventListener() {};
public:
virtual bool EventHandle(EVENT1) = 0;
};
/*
多线程( 1读 多写 )消息/事件分发中心,对应于通常意义的事件总线或者事件驱动引擎,应用层面,方便不同模块数据进行通信。
*/
class EventsCenter
{
private:
static EventsCenter* instance;
static std::mutex m_mutex;
string name;
messenger<EVENT1> * mes;
bool isOpen; //是否关闭
thread *td_dispatch;
mutex m_mutex_update;
list<IEventListener*> list_listeners;
private :
EventsCenter(const EventsCenter&) = delete;
EventsCenter& operator=(const EventsCenter&) = delete;
public:
static EventsCenter* getInstance()
{
if (instance == nullptr)
{
std::lock_guard<std::mutex> guard(m_mutex);
if (instance == nullptr)
instance = new EventsCenter();
}
return instance;
};
class GC {
public:
~GC()
{
if (instance != nullptr)
{
delete instance;
}
};
};
static EventsCenter::GC gc;
//注册事件及其监听者,监听者通过receEvent接口方法获取事件类型及其事件内容。
void addEventListener(IEventListener *listener)
{
lock_guard<mutex> lk(m_mutex_update);
list_listeners.push_back(listener);
}
//发送事件
void sendEvent(EVENT1 ev1)
{
/*EVENT1 ev1;
if (index_1 % 2 == 0)
{
TICK t;
strcpy(t.name, "ax1");
t.price = 1 + index_1;
t.volumn = index_1;
ev1.type = EVENT_TYPE::EVENT_TICK;
ev1.data.tick = t;
}
else
{
ORDER t;
strcpy(t.name, "or1");
t.price = 1 + index_1;
t.volumn = index_1;
ev1.type = EVENT_TYPE::EVENT_ORDER;
ev1.data.order = t;
}*/
mes->notify(ev1);
}
//事件分发,采用类似广播方式,不进行区分消息类型,由接收者自己判断消息类型来处理
void eventDispatchLoop()
{
while (true)
{
EVENT1 result = mes->wait();
cout << getSystemClock_microSeconds() << __FUNCTION__ << " " << name << endl;
if (isOpen == false)
{
cout << getSystemClock_microSeconds() << __FUNCTION__ << " closed "<<endl;
break;
}
if (result.type != EVENT_TYPE::EVENT_NONE)
{
lock_guard<mutex> lk(m_mutex_update);
bool isContinue=false;
for(list<IEventListener*>::const_iterator listener = list_listeners.cbegin(); listener!=list_listeners.cend(); listener++)
{
isContinue=(*listener)->EventHandle(result);
if (isContinue == false)
break;
}
}
}
}
//运行事件中心。
void Run()
{
isOpen = true;
td_dispatch= new thread(&EventsCenter::eventDispatchLoop, this);
td_dispatch->detach();
}
//关闭事件中心。
void Close()
{
isOpen = false;
delete td_dispatch;
td_dispatch = nullptr;
}
void Init(string str)
{
name = str;
mes = new messenger<EVENT1>();
}
EventsCenter()
{
}
~EventsCenter()
{
delete mes;
}
};
EventsCenter::GC EventsCenter::gc;
EventsCenter* EventsCenter::instance;
mutex EventsCenter::m_mutex;
class TestTickListener : public IEventListener
{
public:
virtual bool EventHandle(EVENT1 result) override
{
if (result.type == EVENT_TYPE::EVENT_TICK)
cout << getSystemClock_microSeconds() << __FUNCTION__ << " result " << result.type << " " << result.data.tick.name << " " << result.data.tick.volumn << endl;
return true;
}
};
class TestOrderListener : public IEventListener
{
public:
virtual bool EventHandle(EVENT1 result) override
{
if (result.type != EVENT_TYPE::EVENT_ORDER)
return true;
cout << getSystemClock_microSeconds() << __FUNCTION__ << " result " << result.type << " " << result.data.tick.name << " " << result.data.order.SysId << endl;
cout << getSystemClock_microSeconds() << __FUNCTION__ << " result " << result.type << " " << result.data.tick.name << " " << result.data.order.SysId << endl;
return true;
}
};
#define TEST_SLEEP_SECONDS(timeSpan) std::this_thread::sleep_for(std::chrono::seconds(timeSpan));
void eventsTest()
{
EventsCenter::getInstance()->Init("eventsTest");
EventsCenter::getInstance()->Run();
TestTickListener tickListener;
EventsCenter::getInstance()->addEventListener(&tickListener);
TestOrderListener *orderListener = new TestOrderListener();
EventsCenter::getInstance()->addEventListener(orderListener);
EventsCenter::getInstance()->Run();
TEST_SLEEP_SECONDS(1);
EVENT1 ev1;
TICK t;
strcpy(t.name, "ax1");
t.price = 1 ;
t.volumn = 1;
ev1.type = EVENT_TYPE::EVENT_TICK;
ev1.data.tick = t;
EventsCenter::getInstance()->sendEvent(ev1);
TEST_SLEEP_SECONDS(2);
ORDER order;
strcpy(order.name, "or1");
order.price = 2;
strcpy(order.SysId,"11111");
ev1.type = EVENT_TYPE::EVENT_ORDER;
ev1.data.order = order;
EventsCenter::getInstance()->sendEvent(ev1);
TEST_SLEEP_SECONDS(2);
EventsCenter::getInstance()->Close();
delete orderListener;
}