├── README.md ├── TinyTimer.hpp └── main.cpp /README.md: -------------------------------------------------------------------------------- 1 | # TinyTimer 2 | TinyTimer是一个只有单个头文件的简单的支持C++11特性的定时器, 3 | 该类包括以下特征: 4 | 5 | - 支持同步/异步执行定时器任务; 6 | 7 | - 支持异步一次或者多次执行定时器任务 8 | 9 | - 支持定时器即时退出,并不会阻塞主线程; 10 | 11 | 12 | 13 | # 代码演示 14 | ## 1 静态/全局作为任务函数 15 | 16 | ```cpp 17 | #include 18 | #include "conio.h" 19 | 20 | #include "TinyTimer.hpp" 21 | 22 | using namespace std; 23 | 24 | void Print() 25 | { 26 | std::cout << "定时器全局函数Print执行" << std::endl; 27 | } 28 | 29 | 30 | int main() 31 | { 32 | TinyTimer::ptr tinyTimer = std::make_shared(); 33 | 34 | // 延迟1秒执行1次静态/全局函数 35 | tinyTimer->AsyncOnceExecute(1000, Print); 36 | tinyTimer->KillTimer(); 37 | 38 | // 循环执行静态/全局函数,按Q键杀死定时器 39 | int a = 0; 40 | while (true) 41 | { 42 | // 在此处填入需要循环的代码 43 | if (a == 0) 44 | { 45 | tinyTimer->AsyncLoopExecute(1, Print); 46 | } 47 | 48 | if (_kbhit()) // 如果有按键被按下 49 | { 50 | if (_getch() == 'q') //如果按下了q键则跳出循环 51 | { 52 | tinyTimer->KillTimer(); 53 | break; 54 | } 55 | 56 | } 57 | } 58 | 59 | getchar(); 60 | return 0; 61 | } 62 | ``` 63 | 64 | ## 2 Lambda表达式作为任务函数 65 | 66 | ```cpp 67 | #include 68 | #include "conio.h" 69 | 70 | #include "TinyTimer.hpp" 71 | 72 | using namespace std; 73 | 74 | int main() 75 | { 76 | TinyTimer::ptr tinyTimer = std::make_shared(); 77 | 78 | // 延迟1秒执行lambda函数表达式 79 | tinyTimer->AsyncOnceExecute(1000, []() { 80 | std::cout << "Lambda函数执行" << std::endl; 81 | }); 82 | tinyTimer->KillTimer(); 83 | 84 | getchar(); 85 | return 0; 86 | } 87 | ``` 88 | 89 | ## 3 类成员函数做为任务函数 90 | 91 | ```cpp 92 | #include 93 | #include "conio.h" 94 | 95 | #include "TinyTimer.hpp" 96 | 97 | using namespace std; 98 | 99 | void Print() 100 | { 101 | std::cout << "定时器全局函数Print执行" << std::endl; 102 | } 103 | 104 | 105 | class Task 106 | { 107 | public: 108 | Task() { 109 | 110 | }; 111 | 112 | ~Task() { 113 | 114 | }; 115 | 116 | void TaskRun() 117 | { 118 | std::cout << "Task::TaskRun()函数执行" << std::endl; 119 | }; 120 | }; 121 | 122 | 123 | int main() 124 | { 125 | TinyTimer::ptr tinyTimer = std::make_shared(); 126 | 127 | // 延迟1秒执行1次类成员函数 128 | Task taskObj; 129 | std::function taskRunFunc = std::bind(&Task::TaskRun, taskObj); 130 | tinyTimer->AsyncOnceExecute(1000, taskRunFunc); 131 | tinyTimer->KillTimer(); 132 | 133 | getchar(); 134 | return 0; 135 | } 136 | ``` 137 | -------------------------------------------------------------------------------- /TinyTimer.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HW140701/TinyTimer/6dc674b98fd16a500efc35d74d43c0897c48eec2/TinyTimer.hpp -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "conio.h" 3 | 4 | #include "TinyTimer.hpp" 5 | 6 | using namespace std; 7 | 8 | void Print() 9 | { 10 | std::cout << "定时器全局函数Print执行" << std::endl; 11 | } 12 | 13 | 14 | class Task 15 | { 16 | public: 17 | Task() { 18 | 19 | }; 20 | 21 | ~Task() { 22 | 23 | }; 24 | 25 | void TaskRun() 26 | { 27 | std::cout << "Task::TaskRun()函数执行" << std::endl; 28 | }; 29 | }; 30 | 31 | 32 | int main() 33 | { 34 | TinyTimer::ptr tinyTimer = std::make_shared(); 35 | 36 | // 延迟1秒执行1次静态/全局函数 37 | tinyTimer->AsyncOnceExecute(1000, Print); 38 | tinyTimer->KillTimer(); 39 | 40 | // 延迟1秒执行lambda函数表达式 41 | tinyTimer->AsyncOnceExecute(1000, []() { 42 | std::cout << "Lambda函数执行" << std::endl; 43 | }); 44 | tinyTimer->KillTimer(); 45 | 46 | // 延迟1秒执行1次类成员函数 47 | Task taskObj; 48 | std::function taskRunFunc = std::bind(&Task::TaskRun, taskObj); 49 | tinyTimer->AsyncOnceExecute(1000, taskRunFunc); 50 | tinyTimer->KillTimer(); 51 | 52 | // 循环执行静态/全局函数,按Q键杀死定时器 53 | int a = 0; 54 | while (true) 55 | { 56 | // 在此处填入需要循环的代码 57 | if (a == 0) 58 | { 59 | tinyTimer->AsyncLoopExecute(1, Print); 60 | } 61 | 62 | if (_kbhit()) // 如果有按键被按下 63 | { 64 | if (_getch() == 'q') //如果按下了q键则跳出循环 65 | { 66 | tinyTimer->KillTimer(); 67 | break; 68 | } 69 | 70 | } 71 | } 72 | 73 | getchar(); 74 | return 0; 75 | } --------------------------------------------------------------------------------