forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_queue.h
More file actions
45 lines (40 loc) · 1.23 KB
/
task_queue.h
File metadata and controls
45 lines (40 loc) · 1.23 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
#pragma once
#include <memory>
#include <string>
#include "trantor/net/EventLoopThreadPool.h"
namespace cortex {
class TaskQueue {
public:
TaskQueue(size_t num_threads, const std::string& name)
: ev_loop_pool_(
std::make_unique<trantor::EventLoopThreadPool>(num_threads, name)) {
ev_loop_pool_->start();
}
~TaskQueue() {}
template <typename Functor>
void RunInQueue(Functor&& f) {
if (ev_loop_pool_) {
ev_loop_pool_->getNextLoop()->runInLoop(std::forward<Functor>(f));
}
}
template <typename Functor>
uint64_t RunEvery(const std::chrono::duration<double>& interval,
Functor&& cb) {
if (ev_loop_pool_) {
return ev_loop_pool_->getNextLoop()->runEvery(interval,
std::forward<Functor>(cb));
}
return 0;
}
template <typename Functor>
uint64_t RunAfter(const std::chrono::duration<double>& delay, Functor&& cb) {
if (ev_loop_pool_) {
return ev_loop_pool_->getNextLoop()->runAfter(delay,
std::forward<Functor>(cb));
}
return 0;
}
private:
std::unique_ptr<trantor::EventLoopThreadPool> ev_loop_pool_ = nullptr;
};
} // namespace cortex