诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

50 行
1.2 KiB

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