Runnable.h 951 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef __RUNNABLE__HH
  2. #define __RUNNABLE__HH
  3. #include <thread>
  4. #include <cstdint>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <iostream>
  9. #include <exception>
  10. class CRunnable
  11. {
  12. private:
  13. enum THREAD_STATUS { RUNNING, SUSPENDED, STOPPED };
  14. public:
  15. CRunnable(const std::string threadName = "noNamed");
  16. ~CRunnable() {}
  17. bool Start(bool suspend = false);
  18. int Join(int timeout = -1);
  19. public:
  20. virtual void Run() = 0;
  21. public:
  22. bool isRunning() { return (RUNNING == status); }
  23. bool isSuspended() { return (SUSPENDED == status); }
  24. bool isStopped() { return (STOPPED == status); }
  25. bool isAlive() { return (STOPPED != status); }
  26. THREAD_STATUS getStatus() { return status; };
  27. private:
  28. void setStatus(THREAD_STATUS status) { this->status = status; }
  29. static void staticThreadFunc(void* arg);
  30. protected:
  31. std::thread* m_pthread;
  32. volatile THREAD_STATUS status;
  33. };
  34. #endif