添加startOne方法
This commit is contained in:
@@ -37,6 +37,7 @@ namespace ling {
|
|||||||
static std::atomic<bool> flag;
|
static std::atomic<bool> flag;
|
||||||
static std::condition_variable listCV;
|
static std::condition_variable listCV;
|
||||||
|
|
||||||
|
std::atomic<bool> state{true};
|
||||||
std::function<void()> recall;
|
std::function<void()> recall;
|
||||||
int64_t id = 0;
|
int64_t id = 0;
|
||||||
Task task;
|
Task task;
|
||||||
@@ -57,14 +58,18 @@ namespace ling {
|
|||||||
args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误
|
args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误
|
||||||
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
||||||
std::function<void()> warpper_func = [task_ptr, this]() {
|
std::function<void()> warpper_func = [task_ptr, this]() {
|
||||||
(*task_ptr)();
|
if (this->state.load()) {
|
||||||
this->recall();
|
this->recall();
|
||||||
|
}
|
||||||
|
(*task_ptr)();
|
||||||
};
|
};
|
||||||
this->recall = [this, func]() {
|
this->recall = [this, func]() {
|
||||||
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
|
||||||
std::function<void()> warpper_func = [task_ptr, this]() {
|
std::function<void()> warpper_func = [task_ptr, this]() {
|
||||||
(*task_ptr)();
|
if (this->state.load()) {
|
||||||
this->recall();
|
this->recall();
|
||||||
|
}
|
||||||
|
(*task_ptr)();
|
||||||
};
|
};
|
||||||
this->task.time = std::time(nullptr) + this->task.interval;
|
this->task.time = std::time(nullptr) + this->task.interval;
|
||||||
this->task.fun = warpper_func;
|
this->task.fun = warpper_func;
|
||||||
@@ -82,6 +87,8 @@ namespace ling {
|
|||||||
/// 重新启动定时器
|
/// 重新启动定时器
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
|
void startOne(int64_t interval);
|
||||||
|
|
||||||
void stop() const;
|
void stop() const;
|
||||||
|
|
||||||
/// 设置任务执行器
|
/// 设置任务执行器
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ namespace ling {
|
|||||||
Task task = list.front();
|
Task task = list.front();
|
||||||
std::time_t time = std::time(nullptr);
|
std::time_t time = std::time(nullptr);
|
||||||
if (task.time <= time) {
|
if (task.time <= time) {
|
||||||
call(task.fun);
|
|
||||||
list.pop_front();
|
list.pop_front();
|
||||||
|
call(task.fun);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,6 +76,7 @@ namespace ling {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Timer::start(int64_t interval) {
|
void Timer::start(int64_t interval) {
|
||||||
|
this->state.store(true);
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex);
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
for (auto it = list.begin(); it != list.end(); ++it) {
|
for (auto it = list.begin(); it != list.end(); ++it) {
|
||||||
@@ -101,6 +102,24 @@ namespace ling {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Timer::startOne(int64_t interval) {
|
||||||
|
this->state.store(false);
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
for (auto it = list.begin(); it != list.end(); ++it) {
|
||||||
|
if (it->id == this->id) {
|
||||||
|
list.erase(it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this->task.time = std::time(nullptr) + interval;
|
||||||
|
this->task.interval = interval;
|
||||||
|
addTask(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Timer::start() {
|
void Timer::start() {
|
||||||
start(this->task.interval);
|
start(this->task.interval);
|
||||||
}
|
}
|
||||||
@@ -108,5 +127,4 @@ namespace ling {
|
|||||||
Timer::~Timer() {
|
Timer::~Timer() {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // ling
|
} // ling
|
||||||
Reference in New Issue
Block a user