Thread.h 996 B

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