Agora  1.2.0
Agora project
periodic_worker-inl.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 #ifndef SPDLOG_HEADER_ONLY
8 #endif
9 
10 namespace spdlog {
11 namespace details {
12 
13 SPDLOG_INLINE periodic_worker::periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval)
14 {
15  active_ = (interval > std::chrono::seconds::zero());
16  if (!active_)
17  {
18  return;
19  }
20 
21  worker_thread_ = std::thread([this, callback_fun, interval]() {
22  for (;;)
23  {
24  std::unique_lock<std::mutex> lock(this->mutex_);
25  if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
26  {
27  return; // active_ == false, so exit this thread
28  }
29  callback_fun();
30  }
31  });
32 }
33 
34 // stop the worker thread and join it
36 {
37  if (worker_thread_.joinable())
38  {
39  {
40  std::lock_guard<std::mutex> lock(mutex_);
41  active_ = false;
42  }
43  cv_.notify_one();
44  worker_thread_.join();
45  }
46 }
47 
48 } // namespace details
49 } // namespace spdlog
spdlog::details::periodic_worker::periodic_worker
periodic_worker(const std::function< void()> &callback_fun, std::chrono::seconds interval)
Definition: periodic_worker-inl.h:13
spdlog::details::periodic_worker::mutex_
std::mutex mutex_
Definition: periodic_worker.h:32
periodic_worker.h
spdlog::details::periodic_worker::~periodic_worker
~periodic_worker()
Definition: periodic_worker-inl.h:35
spdlog::details::periodic_worker::cv_
std::condition_variable cv_
Definition: periodic_worker.h:33
spdlog
Definition: async.h:25
SPDLOG_INLINE
#define SPDLOG_INLINE
Definition: common.h:42
spdlog::details::periodic_worker::worker_thread_
std::thread worker_thread_
Definition: periodic_worker.h:31
function
function[avg_proc_duration, std_proc_duration]
Definition: parse_dl_file.m:1
spdlog::details::periodic_worker::active_
bool active_
Definition: periodic_worker.h:30