spdlog/include/spdlog/details/periodic_worker.h

41 lines
1.1 KiB
C
Raw Normal View History

2019-06-03 17:09:16 -04:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2018-07-21 19:02:01 -04:00
// periodic worker thread - periodically executes the given callback function.
//
// RAII over the owned thread:
// creates the thread on construction.
2018-11-02 11:53:27 -04:00
// stops and joins the thread on destruction (if the thread is executing a callback, wait for it to finish first).
#include <chrono>
2018-07-21 16:48:07 -04:00
#include <condition_variable>
#include <functional>
2018-07-21 16:48:07 -04:00
#include <mutex>
2018-07-22 14:55:47 -04:00
#include <thread>
2018-07-21 16:48:07 -04:00
namespace spdlog {
namespace details {
class periodic_worker
{
public:
2019-05-08 10:16:56 -04:00
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval);
2018-07-21 16:48:07 -04:00
periodic_worker(const periodic_worker &) = delete;
periodic_worker &operator=(const periodic_worker &) = delete;
2018-07-24 15:59:34 -04:00
// stop the worker thread and join it
2019-05-08 10:16:56 -04:00
~periodic_worker();
private:
2018-07-23 20:08:49 -04:00
bool active_;
2018-07-24 15:59:34 -04:00
std::thread worker_thread_;
std::mutex mutex_;
std::condition_variable cv_;
};
2018-07-21 16:48:07 -04:00
} // namespace details
2018-07-21 19:02:01 -04:00
} // namespace spdlog
2019-05-08 10:16:56 -04:00
#ifdef SPDLOG_HEADER_ONLY
2019-05-11 06:19:53 -04:00
#include "periodic_worker-inl.h"
2019-05-08 10:16:56 -04:00
#endif