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.

threadinterrupt.cpp 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "threadinterrupt.h"
  6. CThreadInterrupt::operator bool() const
  7. {
  8. return flag.load(std::memory_order_acquire);
  9. }
  10. void CThreadInterrupt::reset()
  11. {
  12. flag.store(false, std::memory_order_release);
  13. }
  14. void CThreadInterrupt::operator()()
  15. {
  16. {
  17. std::unique_lock<std::mutex> lock(mut);
  18. flag.store(true, std::memory_order_release);
  19. }
  20. cond.notify_all();
  21. }
  22. bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
  23. {
  24. std::unique_lock<std::mutex> lock(mut);
  25. return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
  26. }
  27. bool CThreadInterrupt::sleep_for(std::chrono::seconds rel_time)
  28. {
  29. return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
  30. }
  31. bool CThreadInterrupt::sleep_for(std::chrono::minutes rel_time)
  32. {
  33. return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
  34. }