Thread.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "Thread.h"
  2. #include "Globals.h"
  3. using namespace Framework;
  4. // inhalt der Thread Klasse aus Thread.h
  5. // Konstruktor
  6. Thread::Thread()
  7. {
  8. thRegister->add( this );
  9. #ifdef WIN32
  10. threadHandle = 0;
  11. threadId = 0;
  12. #endif
  13. run = 0;
  14. }
  15. // Destruktor
  16. Thread::~Thread()
  17. {
  18. thRegister->remove( this );
  19. #ifdef WIN32
  20. if( GetCurrentThreadId() == GetThreadId( threadHandle ) )
  21. return;
  22. #else
  23. if( pthread_self() == threadHandle )
  24. return;
  25. #endif
  26. if( run )
  27. warteAufThread( 100000 );
  28. if( run )
  29. ende();
  30. }
  31. // nicht constant
  32. void Thread::start() // startet den Thread
  33. {
  34. if( !run )
  35. {
  36. #ifdef WIN32
  37. threadHandle = CreateThread( 0, 0, threadStart, this, 0, &threadId );
  38. #else
  39. pthread_create( &threadHandle, NULL, threadStart, this );
  40. #endif
  41. }
  42. run = 1;
  43. }
  44. #ifdef WIN32
  45. void Thread::pause() // pausiert den Thread
  46. {
  47. if( run )
  48. SuspendThread( threadHandle );
  49. run = 0;
  50. }
  51. void Thread::fortsetzen() // pausiert den Thread
  52. {
  53. if( !run )
  54. ResumeThread( threadHandle );
  55. run = 1;
  56. }
  57. #endif
  58. void Thread::ende() // beendet den Thread
  59. {
  60. if( run )
  61. {
  62. #ifdef WIN32
  63. #pragma warning(suppress: 6258)
  64. TerminateThread( threadHandle, 0 );
  65. #else
  66. pthread_cancel( threadHandle );
  67. #endif
  68. }
  69. run = 0;
  70. }
  71. void Thread::thread() // Thread
  72. {
  73. }
  74. void Thread::threadEnd()
  75. {
  76. run = 0;
  77. }
  78. // constant
  79. bool Thread::läuft() const // prüft, ob der Thrad aktiv ist
  80. {
  81. return run;
  82. }
  83. int Thread::warteAufThread( int zeit ) const // wartet zeit lang auf den Thread
  84. {
  85. #ifdef WIN32
  86. if( !run )
  87. return WAIT_OBJECT_0;
  88. if( GetCurrentThreadId() == GetThreadId( threadHandle ) )
  89. return WAIT_OBJECT_0;
  90. return WaitForSingleObject( threadHandle, zeit );
  91. #else
  92. if( !run )
  93. return 0;
  94. return pthread_join( threadHandle, 0 );
  95. #endif
  96. }
  97. #ifdef WIN32
  98. void *Thread::getThreadHandle() const
  99. {
  100. return threadHandle;
  101. }
  102. #endif
  103. // funktionen
  104. #ifdef WIN32
  105. unsigned long __stdcall Framework::threadStart( void *param )
  106. {
  107. if( istThreadOk( (Thread *)param ) )
  108. ( (Thread *)param )->thread();
  109. if( istThreadOk( ( Thread *)param ) )
  110. ( (Thread *)param )->threadEnd();
  111. return 0;
  112. }
  113. #else
  114. void *Framework::threadStart( void *param )
  115. {
  116. pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, 0 );
  117. ( (Thread *)param )->thread();
  118. ( (Thread *)param )->threadEnd();
  119. pthread_exit( 0 );
  120. return 0;
  121. }
  122. #endif
  123. // Inhalt der ThreadRegister Klasse aus Thread.h
  124. void ThreadRegister::add( Thread *t )
  125. {
  126. threads.add( t );
  127. }
  128. void ThreadRegister::remove( Thread *t )
  129. {
  130. threads.lösche( threads.getWertIndex( t ) );
  131. }
  132. bool ThreadRegister::isThread( Thread *t ) const
  133. {
  134. return threads.hat( threads.getWertIndex( t ) );
  135. }