Public Member Functions | Private Attributes | List of all members
spdlog::details::periodic_worker Class Reference

#include <periodic_worker.h>

Public Member Functions

 periodic_worker (const std::function< void()> &callback_fun, std::chrono::seconds interval)
 
 periodic_worker (const periodic_worker &)=delete
 
periodic_workeroperator= (const periodic_worker &)=delete
 
 ~periodic_worker ()
 

Private Attributes

bool active_
 
std::thread worker_thread_
 
std::mutex mutex_
 
std::condition_variable cv_
 

Detailed Description

Definition at line 23 of file periodic_worker.h.

Constructor & Destructor Documentation

spdlog::details::periodic_worker::periodic_worker ( const std::function< void()> &  callback_fun,
std::chrono::seconds  interval 
)
inline

Definition at line 26 of file periodic_worker.h.

27  {
28  active_ = (interval > std::chrono::seconds::zero());
29  if (!active_)
30  {
31  return;
32  }
33 
34  worker_thread_ = std::thread([this, callback_fun, interval]() {
35  for (;;)
36  {
37  std::unique_lock<std::mutex> lock(this->mutex_);
38  if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; }))
39  {
40  return; // active_ == false, so exit this thread
41  }
42  callback_fun();
43  }
44  });
45  }
std::condition_variable cv_
spdlog::details::periodic_worker::periodic_worker ( const periodic_worker )
delete
spdlog::details::periodic_worker::~periodic_worker ( )
inline

Definition at line 51 of file periodic_worker.h.

52  {
53  if (worker_thread_.joinable())
54  {
55  {
56  std::lock_guard<std::mutex> lock(mutex_);
57  active_ = false;
58  }
59  cv_.notify_one();
60  worker_thread_.join();
61  }
62  }
std::condition_variable cv_

Member Function Documentation

periodic_worker& spdlog::details::periodic_worker::operator= ( const periodic_worker )
delete

Member Data Documentation

bool spdlog::details::periodic_worker::active_
private

Definition at line 65 of file periodic_worker.h.

std::condition_variable spdlog::details::periodic_worker::cv_
private

Definition at line 68 of file periodic_worker.h.

std::mutex spdlog::details::periodic_worker::mutex_
private

Definition at line 67 of file periodic_worker.h.

std::thread spdlog::details::periodic_worker::worker_thread_
private

Definition at line 66 of file periodic_worker.h.


The documentation for this class was generated from the following file: