From 852acda8d1f672f81cb7e0180a03a3b2b5230be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=BB=E9=AD=82=E5=9C=A3=E4=BD=BF?= Date: Tue, 23 Jan 2024 18:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0startTask=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Timer.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/Timer.h b/include/Timer.h index d5cd8eb..bf4a227 100644 --- a/include/Timer.h +++ b/include/Timer.h @@ -80,6 +80,21 @@ namespace ling { return *this; } + template + static void startTask(F &&f, int64_t interval, Args &&...args) { + std::function func = std::bind(std::forward(f), std::forward( + args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误 + auto task_ptr = std::make_shared>(func); + std::function warpper_func = [task_ptr]() { + (*task_ptr)(); + }; + Task task; + task.id = -1; + task.time = std::time(nullptr) + interval; + task.fun = warpper_func; + addTask(task); + } + /// 启动定时器 void start(int64_t interval);