-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp_main.cpp
More file actions
45 lines (39 loc) · 1.13 KB
/
app_main.cpp
File metadata and controls
45 lines (39 loc) · 1.13 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
#include <tbox/main/module.h>
#include <tbox/base/log.h>
#include <thread>
class MyModule : public tbox::main::Module {
public:
explicit MyModule(tbox::main::Context &ctx)
: tbox::main::Module("my", ctx)
{ }
public:
virtual bool onStart() override {
//! 创建子线程,令其周期性调用runInLoop()
thread_ = std::thread([this] {
int index = 0;
while (keep_running_) {
//! 延时1秒
std::this_thread::sleep_for(std::chrono::seconds(1));
++index;
LogDbg("thread index: %d", index);
//! 委托给Loop线程
ctx().loop()->runInLoop([index] {
LogDbg("loop index: %d", index);
});
}
});
return true;
}
virtual void onStop() override { keep_running_ = false; }
virtual void onCleanup() override { thread_.join(); }
private:
bool keep_running_ = true;
std::thread thread_;
};
namespace tbox {
namespace main {
void RegisterApps(Module &apps, Context &ctx) {
apps.add(new MyModule(ctx));
}
}
}