forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_timer.hpp
68 lines (57 loc) · 2.19 KB
/
sf_timer.hpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @version 1.0.0
* @author skyfire
* @mail skyfireitdiy@hotmail.com
* @see http://github.com/skyfireitdiy/sflib
* @file sf_timer.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
/*
* sf_timer 定时器
*/
#pragma once
#include "sf_timer.h"
namespace skyfire
{
inline void sf_timer::start(int ms, bool once) {
if(running__)
{
return;
}
running__ = true;
std::thread new_thread = std::thread ([=](bool is_once)
{
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
if(std::this_thread::get_id() != current_timer_thread__)
{
return;
}
if (running__)
{
timeout();
if (is_once)
{
running__ = false;
return;
}
}
else
{
break;
}
}
}, once);
current_timer_thread__ = new_thread.get_id();
new_thread.detach();
}
inline bool sf_timer::is_active() {
return running__;
}
inline void sf_timer::stop() {
running__ = false;
}
}