Thread.h 1022 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2014 Art. All Rights Reserved.
  2. #ifndef POSIX_THREAD_H
  3. #define POSIX_THREAD_H
  4. #ifdef __cplusplus
  5. #include <pthread.h>
  6. #include <sched.h>
  7. #include <semaphore.h>
  8. //thread
  9. class ArkThread {
  10. public:
  11. ArkThread() : mRunning(false) { }
  12. virtual ~ArkThread();
  13. bool start();
  14. bool join();
  15. unsigned long id();
  16. bool isRunning();
  17. void yield();
  18. bool setPriority(int priority);
  19. bool setName(const char* name);
  20. protected:
  21. static void* callback(void* arg);
  22. virtual void run() = 0;
  23. private:
  24. pthread_t mThread;
  25. bool mRunning;
  26. };
  27. //lock
  28. class Mutex {
  29. public:
  30. Mutex();
  31. ~Mutex();
  32. void lock();
  33. void unlock();
  34. private:
  35. pthread_mutex_t mMutex;
  36. };
  37. class Autolock {
  38. public:
  39. Autolock(Mutex* mutex);
  40. ~Autolock();
  41. private:
  42. Mutex* mMutex;
  43. };
  44. //sem
  45. class Semaphore {
  46. public:
  47. Semaphore();
  48. Semaphore(int value);
  49. ~Semaphore();
  50. bool down();
  51. bool up();
  52. bool arktry();
  53. bool down(int sec);
  54. private:
  55. sem_t mSem;
  56. };
  57. #endif
  58. #endif