StdCondition.cpp 701 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "StdCondition.h"
  2. StdCondition::StdCondition():m_value(false)
  3. {
  4. }
  5. StdCondition::StdCondition(bool init):m_value(init)
  6. {
  7. }
  8. StdCondition::~StdCondition()
  9. {
  10. }
  11. bool StdCondition::isTrue(StdCondition* scd)
  12. {
  13. if (scd == 0)return false;
  14. return scd->m_value;
  15. }
  16. void StdCondition::Wait()
  17. {
  18. std::unique_lock<std::mutex> loc(m_mutex);
  19. m_cv.wait(loc,std::bind(isTrue,this));
  20. }
  21. bool StdCondition::WaitFor(unsigned int millisecond)
  22. {
  23. std::unique_lock<std::mutex> loc(m_mutex);
  24. return m_cv.wait_for(loc, std::chrono::milliseconds(millisecond), std::bind(isTrue, this));
  25. }
  26. void StdCondition::Notify(bool istrue)
  27. {
  28. std::unique_lock<std::mutex> loc(m_mutex);
  29. m_value = istrue;
  30. m_cv.notify_all();
  31. }