-
Notifications
You must be signed in to change notification settings - Fork 242
/
info_router.cpp
54 lines (45 loc) · 1.35 KB
/
info_router.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
#include <thread>
#include "info_router.h"
#include "func_thread.h"
InfoRouter::InfoRouter() {
}
InfoRouter::~InfoRouter() {
}
void InfoRouter::AddThread(std::shared_ptr<FuncThread>& thread) {
_func_thread_vec.push_back(thread);
thread->Start();
}
void InfoRouter::StopAllThread() {
for (int i = 0; i < (int)_func_thread_vec.size(); i++) {
_func_thread_vec[i]->Stop();
_func_thread_vec[i]->Join();
}
}
void InfoRouter::PushTask(FuncCallInfo* info) {
{
std::unique_lock<std::mutex> lock(_mutex);
_func_thread_vec[_curent_index]->Push(info);
}
_curent_index++;
if (_curent_index > _func_thread_vec.size() - 1) {
_curent_index = _curent_index % (int)_func_thread_vec.size();
}
}
void InfoRouter::PushRet(FuncCallInfo* info) {
_out_task_list.Push(info);
}
FuncCallInfo* InfoRouter::GetRet() {
return _out_task_list.Pop();
}
void InfoRouter::RegisterFunc(const std::string& name, const CommonFunc& func) {
std::unique_lock<std::mutex> lock(_mutex);
for (int i = 0; i < (int)_func_thread_vec.size(); i++) {
_func_thread_vec[i]->RegisterFunc(name, func);
}
}
void InfoRouter::RemoveFunc(const std::string& name) {
std::unique_lock<std::mutex> lock(_mutex);
for (int i = 0; i < (int)_func_thread_vec.size(); i++) {
_func_thread_vec[i]->RemoveFunc(name);
}
}