Thread.h 3.8 KB

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