9 #ifndef WRAPPER_COMMON_TIMER_H_ 10 #define WRAPPER_COMMON_TIMER_H_ 19 #include <condition_variable> 37 void StartTimer(
int interval, std::function<
void()> task) {
43 std::thread([
this, interval, task]() {
45 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
50 std::lock_guard<std::mutex> locker(
mutex_);
68 std::unique_lock<std::mutex> locker(
mutex_);
77 template<
typename callable,
class... arguments>
78 void SyncWait(
int after, callable&& f, arguments&&... args) {
80 std::function<
typename std::result_of<callable(arguments...)>::type()> task
81 (std::bind(std::forward<callable>(
f), std::forward<arguments>(args)...));
82 std::this_thread::sleep_for(std::chrono::milliseconds(after));
85 template<
typename callable,
class... arguments>
86 void AsyncWait(
int after, callable&& f, arguments&&... args) {
87 std::function<
typename std::result_of<callable(arguments...)>::type()> task
88 (std::bind(std::forward<callable>(
f), std::forward<arguments>(args)...));
90 std::thread([after, task]() {
91 std::this_thread::sleep_for(std::chrono::milliseconds(after));
105 ThreadPool(
int max_thread_count = 10) : max_thread_count_(max_thread_count), total_thread_count_(0), free_thread_count_(0) {};
116 std::unique_lock<std::mutex> locker(
mutex_);
117 total_thread_count_ += 1;
118 free_thread_count_ += 1;
124 std::function<void()> task;
126 task_cond_.wait(locker, [
this] {
127 return stoped_ || !task_que_.empty();
129 if (stoped_ && task_que_.empty()) {
133 free_thread_count_ -= 1;
134 task = std::move(task_que_.front());
139 free_thread_count_ += 1;
143 total_thread_count_ -= 1;
144 free_thread_count_ -= 1;
151 task_cond_.notify_all();
152 for (std::thread& thread : pool_) {
154 if (thread.joinable()) thread.join();
161 max_thread_count_ = max_thread_count;
164 template<
typename callable,
class... arguments>
166 std::function<
typename std::result_of<callable(arguments...)>::type()> task
167 (std::bind(std::forward<callable>(
f), std::forward<arguments>(args)...));
169 std::lock_guard<std::mutex> lock{
mutex_ };
170 task_que_.emplace([task]() {
174 task_cond_.notify_one();
178 template<
typename callable,
class... arguments>
179 void commit(callable&& f, arguments&&... args) {
180 std::function<
typename std::result_of<callable(arguments...)>::type()> task
181 (std::bind(std::forward<callable>(
f), std::forward<arguments>(args)...));
187 if (free_thread_count_ == 0 && (max_thread_count_ < 0 || total_thread_count_ < max_thread_count_)) {
188 pool_.emplace_back(std::thread(thread_handle,
this));
193 using Task = std::function<void()>;
205 #endif // WRAPPER_COMMON_TIMER_H_
std::queue< Task > task_que_
std::vector< std::thread > pool_
static void thread_handle(void *arg)
std::atomic< bool > expired_
std::condition_variable expired_cond_
void StartTimer(int interval, std::function< void()> task)
std::atomic< bool > try_to_expire_
ThreadPool(int max_thread_count=10)
void _thread_process(void)
void dispatch(callable &&f, arguments &&...args)
void SyncWait(int after, callable &&f, arguments &&...args)
std::function< void()> Task
std::condition_variable task_cond_
void set_max_thread_count(int max_thread_count)
void commit(callable &&f, arguments &&...args)
std::atomic< bool > stoped_
void AsyncWait(int after, callable &&f, arguments &&...args)