-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp_main.cpp
More file actions
44 lines (39 loc) · 1.03 KB
/
app_main.cpp
File metadata and controls
44 lines (39 loc) · 1.03 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
#include <tbox/main/module.h>
#include <tbox/base/log.h>
#include <tbox/eventx/work_thread.h>
//! CPU密集运算
uint64_t Fibonacci(int n) {
if (n <= 1) return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
class MyModule : public tbox::main::Module {
public:
explicit MyModule(tbox::main::Context &ctx)
: tbox::main::Module("my", ctx)
{ }
public:
virtual bool onStart() {
int n = 40;
LogTrace("start caculate fibonacci %d", n);
//! 委托线程池执行任务
worker_.execute(
[n] {
//! 在线程池中执行
LogTrace("fibonacci %d calculating", n);
auto result = Fibonacci(n); //! 进行CPU密集的运算
LogTrace("caculate fibonacci %d result: %llu", n, result);
}
);
LogTag();
return true;
}
private:
tbox::eventx::WorkThread worker_;
};
namespace tbox {
namespace main {
void RegisterApps(Module &apps, Context &ctx) {
apps.add(new MyModule(ctx));
}
}
}