You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1KB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. // periodic worker thread - periodically executes the given callback function.
  5. //
  6. // RAII over the owned thread:
  7. // creates the thread on construction.
  8. // stops and joins the thread on destruction (if the thread is executing a callback, wait for it to finish first).
  9. #include <chrono>
  10. #include <condition_variable>
  11. #include <functional>
  12. #include <mutex>
  13. #include <thread>
  14. namespace spdlog {
  15. namespace details {
  16. class periodic_worker
  17. {
  18. public:
  19. periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval);
  20. periodic_worker(const periodic_worker &) = delete;
  21. periodic_worker &operator=(const periodic_worker &) = delete;
  22. // stop the worker thread and join it
  23. ~periodic_worker();
  24. private:
  25. bool active_;
  26. std::thread worker_thread_;
  27. std::mutex mutex_;
  28. std::condition_variable cv_;
  29. };
  30. } // namespace details
  31. } // namespace spdlog
  32. #ifdef SPDLOG_HEADER_ONLY
  33. #include "periodic_worker-inl.h"
  34. #endif