diff options
| author | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-22 17:47:07 +0900 |
|---|---|---|
| committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2025-03-22 17:58:43 +0900 |
| commit | 3143d2d40ab5af1053596cd9b46c7034f22c4624 (patch) | |
| tree | 7ffa5b5287bb01125c5d5e313a5e392d1fe82d61 /src | |
| parent | edd8dbb5deeb0d86d0ab9713cf954f0667658d60 (diff) | |
| download | tqt-3143d2d40ab5af1053596cd9b46c7034f22c4624.tar.gz tqt-3143d2d40ab5af1053596cd9b46c7034f22c4624.zip | |
Fix usage of condition variable in TQThread which could lead to a thread being run multiple times in parallel.
Although highly unlikely, the following situation was possible:
- a thread is restarted while still running. The call to the second 'start()' blocks because the thread is still running
- the OS decides to randomly wake up the thread that called 'start()'
- a second thread is started and runs in parallel to the first thread, with obvious undefined behavior
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'src')
| -rw-r--r-- | src/kernel/tqthread_unix.cpp | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/kernel/tqthread_unix.cpp b/src/kernel/tqthread_unix.cpp index 714b35c31..a84663df6 100644 --- a/src/kernel/tqthread_unix.cpp +++ b/src/kernel/tqthread_unix.cpp @@ -103,7 +103,7 @@ void TQThreadInstance::init(unsigned int stackSize) orphan = FALSE; disableThreadPostedEvents = FALSE; - pthread_cond_init(&thread_done, NULL); + pthread_cond_init(&thread_done, nullptr); thread_id = 0; eventLoop = 0; @@ -316,9 +316,11 @@ void TQThread::usleep( unsigned long usecs ) void TQThread::start(Priority priority) { TQMutexLocker locker( d->mutex() ); + while ( d->running ) + { + pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); + } - if ( d->running ) - pthread_cond_wait(&d->thread_done, &locker.mutex()->d->handle); d->running = TRUE; d->finished = FALSE; |
