Thread.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. void Thread::threadEnd()
  74. {
  75. run = 0;
  76. }
  77. // constant
  78. bool Thread::läuft() const // prüft, ob der Thrad aktiv ist
  79. {
  80. return run;
  81. }
  82. int Thread::warteAufThread( int zeit ) const // wartet zeit lang auf den Thread
  83. {
  84. #ifdef WIN32
  85. if( !run )
  86. return WAIT_OBJECT_0;
  87. if( GetCurrentThreadId() == GetThreadId( threadHandle ) )
  88. return WAIT_OBJECT_0;
  89. return WaitForSingleObject( threadHandle, zeit );
  90. #else
  91. if( !run )
  92. return 0;
  93. return pthread_join( threadHandle, 0 );
  94. #endif
  95. }
  96. #ifdef WIN32
  97. void *Thread::getThreadHandle() const
  98. {
  99. return threadHandle;
  100. }
  101. #endif
  102. // funktionen
  103. #ifdef WIN32
  104. unsigned long __stdcall Framework::threadStart( void *param )
  105. {
  106. if( istThreadOk( (Thread *)param ) )
  107. ( (Thread *)param )->thread();
  108. if( istThreadOk( (Thread *)param ) )
  109. ( (Thread *)param )->threadEnd();
  110. return 0;
  111. }
  112. #else
  113. void *Framework::threadStart( void *param )
  114. {
  115. pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, 0 );
  116. ( (Thread *)param )->thread();
  117. ( (Thread *)param )->threadEnd();
  118. pthread_exit( 0 );
  119. return 0;
  120. }
  121. #endif
  122. // Inhalt der ThreadRegister Klasse aus Thread.h
  123. void ThreadRegister::add( Thread *t )
  124. {
  125. threads.add( t );
  126. }
  127. void ThreadRegister::remove( Thread *t )
  128. {
  129. threads.lösche( threads.getWertIndex( t ) );
  130. }
  131. bool ThreadRegister::isThread( Thread *t ) const
  132. {
  133. return threads.hat( threads.getWertIndex( t ) );
  134. }