Runnable.cpp 628 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "Runnable.h"
  2. CRunnable::CRunnable(const std::string threadName/*="noNamed"*/)
  3. :status(STOPPED)
  4. ,m_pthread(0)
  5. {
  6. }
  7. bool CRunnable::Start(bool suspend/*=false*/)
  8. {
  9. m_pthread = new std::thread(&CRunnable::staticThreadFunc,this);
  10. return (m_pthread!=0);
  11. }
  12. int CRunnable::Join(int timeout /*=-1*/)
  13. {
  14. if(m_pthread)
  15. {
  16. if(m_pthread->joinable())
  17. {
  18. m_pthread->join();
  19. return 0;
  20. }
  21. }
  22. else
  23. {
  24. return -1;
  25. }
  26. }
  27. /*static*/ void CRunnable::staticThreadFunc(void *arg)
  28. {
  29. CRunnable* runnable = (CRunnable*)arg;
  30. runnable->Run();
  31. }