Thread.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef Thread_H
  2. #define Thread_H
  3. #include <condition_variable>
  4. #include "Array.h"
  5. #include "ReferenceCounter.h"
  6. namespace Framework
  7. {
  8. class Thread;
  9. class Critical;
  10. #ifdef WIN32
  11. //! This function is used by the Thread class to start a thread
  12. DLLEXPORT unsigned long __stdcall threadStart(void* param);
  13. #else
  14. //! This function is used by the Thread class to start a thread
  15. void* threadStart(void* param); //! starts thread
  16. #endif
  17. //! A new thread similar to the Thread class from Java
  18. class Thread : public virtual ReferenceCounter
  19. {
  20. private:
  21. pthread_t* threadHandleSys;
  22. int* lockCount;
  23. const char* name;
  24. protected:
  25. unsigned long threadId;
  26. pthread_t threadHandle;
  27. bool run;
  28. public:
  29. //! Constructor
  30. DLLEXPORT Thread();
  31. //! Destructor
  32. DLLEXPORT virtual ~Thread();
  33. //! Starts the new thread
  34. DLLEXPORT void start();
  35. #ifdef WIN32
  36. //! Pauses the thread (Windows only)
  37. DLLEXPORT void pause();
  38. //! Resumes the thread (Windows only)
  39. DLLEXPORT void resume();
  40. #endif
  41. //! Terminates the thread
  42. DLLEXPORT void ende();
  43. //! This function is executed by the new thread
  44. DLLEXPORT virtual void thread();
  45. //! This function is called after the thread function has finished
  46. DLLEXPORT virtual void threadEnd();
  47. //! Checks whether the thread is active
  48. //! return: true if the thread is running.
  49. //! false if the thread is terminated, paused or not yet
  50. //! started.
  51. DLLEXPORT bool isRunning() const;
  52. //! Waits for the thread for the specified time
  53. //! \param zeit The time to wait for the thread. 1000 = 1 second
  54. DLLEXPORT int waitForThread(int zeit);
  55. //! Sets a framework pointer to a thread handle that will be set to 0
  56. //! if the thread resources have already been fully cleaned up
  57. //! \param ths A pointer to a thread handle to be modified
  58. DLLEXPORT void setSystemHandlePointer(pthread_t* ths);
  59. //! Returns a handle to the thread
  60. DLLEXPORT pthread_t getThreadHandle() const;
  61. //! set the name of the thread
  62. DLLEXPORT void setName(const char* name);
  63. //! get the name of the thread
  64. DLLEXPORT const char* getName() const;
  65. private:
  66. void setLockCount(int* lockCount);
  67. #ifdef WIN32
  68. //! This function is used by the Thread class to start a thread
  69. friend DLLEXPORT unsigned long __stdcall threadStart(void* param);
  70. #else
  71. //! This function is used by the Thread class to start a thread
  72. friend void* threadStart(void* param); //! starts thread
  73. #endif
  74. };
  75. //! A class that stores all currently running Thread objects
  76. class ThreadRegister
  77. {
  78. private:
  79. Array<Framework::Thread*> threads;
  80. CRITICAL_SECTION cs;
  81. Array<pthread_t> closedThreads;
  82. public:
  83. //! Constructor
  84. ThreadRegister();
  85. //! Destructor
  86. ~ThreadRegister();
  87. //! Adds a new thread
  88. //! \param t The thread to be added
  89. void add(Thread* t);
  90. //! Removes a thread
  91. //! \param t The thread to be removed
  92. void remove(Thread* t);
  93. //! Checks whether a pointer points to a valid Thread object, or
  94. //! whether it has already been deleted \param t The pointer to be
  95. //! checked
  96. bool isThread(Thread* t);
  97. //! Searches for a specific thread and returns the corresponding object
  98. //! handle: A handle to the sought thread
  99. Thread* zThread(pthread_t handle);
  100. //! Called automatically when a thread is terminated. The
  101. //! resources are then released in cleanUpClosedThreads.
  102. //! \param handle The handle of the thread
  103. void addClosedThread(pthread_t handle);
  104. //! Locks the register
  105. void lock();
  106. //! Unlocks the register
  107. void unlock();
  108. //! Deletes the already terminated threads and releases their resources
  109. DLLEXPORT void cleanUpClosedThreads();
  110. };
  111. } // namespace Framework
  112. #endif