PDA

View Full Version : How to sync threads at start-up



Artschi
23rd May 2006, 10:08
Hi folks,

when I start a thread using QThread::start() this method returns immediately after the thread has been created. A signal is sent as well.

Unfortunately, the calling instance has no idea about the internal (init) state of the thread, even if it received the started() signal. In some cases it would be helpful to get synchronized when the thread has completed his initialization.

Is there any (standard) way to delay the stasrt() method and/or the started() signal until all thread internal initialization has been completed? Or do I have to provide an additional custom signal?

Are there any suggestions?
Thanks

wysota
23rd May 2006, 10:17
Two solutions:
1. Create your QThread object and leave it alone and call start() when you want it.
2. Create a mutex, lock it with the main thread, create the worker thread and make it try to lock the same mutex on the very beginning of its run() method. When you call start, the thread will stop immediately, because the mutex is locked. When you want the thread to continue, just release the lock in the main thread.

Artschi
23rd May 2006, 15:20
Right.

This is about how to start the thread processing at a defined time.

Additionaly I'm looking for a solution to inform the main thread about the completion of the initialization work inside the newly created thread. (This thread initialization could cover the creation of internal objects, reading of specific configuration, establishing connections to subsystems, etc.) With other words, the main thread wants to know when the worker thread is ready to process data.

Prefered solution (if possible):
1. Is there a way to delay the started() signal until initialization is done?

something like ...
void MyThread::run() {
// do some thread initialization
emit started();

QThread::exec();
}

Known second class alternatives:
2. Add a new signal (e.g. void MyThread::initialized()), which can be used instead of started().
3. Use a Mutex to synchronize the main thread with the new one.

wysota
23rd May 2006, 16:06
Prefered solution (if possible):
1. Is there a way to delay the started() signal until initialization is done?
something like ...
void MyThread::run() {
// do some thread initialization
emit started();

QThread::exec();
}
You can block signals (QObject::blockSignals()) and reemit that signal when you're ready.


Known second class alternatives:
2. Add a new signal (e.g. void MyThread::initialized()), which can be used instead of started().
3. Use a Mutex to synchronize the main thread with the new one.

I'd add an own signal and immediately after emitting it, try to lock the mutex (which should already be locked by the main thread at that time). The thread would go to sleep until the mutex was unlocked.

Artschi
23rd May 2006, 17:26
You can block signals (QObject::blockSignals()) and reemit that signal when you're ready.

That's what I was looking for!
Thanks

iGoo
25th May 2006, 11:18
sync. of Process or Thread.

use Semaphore or other sync. mechnism.

Mutex,Event,Semaphore,even Signal.