Results 1 to 6 of 6

Thread: How to sync threads at start-up

  1. #1
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to sync threads at start-up

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to sync threads at start-up

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sync threads at start-up

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to sync threads at start-up

    Quote Originally Posted by Artschi
    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.

  5. The following user says thank you to wysota for this useful post:

    Artschi (23rd May 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: How to sync threads at start-up

    Quote Originally Posted by wysota
    You can block signals (QObject::blockSignals()) and reemit that signal when you're ready.
    That's what I was looking for!
    Thanks

  7. #6
    Join Date
    May 2006
    Posts
    32
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Smile Re: How to sync threads at start-up

    sync. of Process or Thread.

    use Semaphore or other sync. mechnism.

    Mutex,Event,Semaphore,even Signal.

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57
  2. [QT4] threads, signals, and slots, please help.
    By ucntcme in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 14:23
  3. Problems with threads and windows
    By SkripT in forum Qt Programming
    Replies: 15
    Last Post: 16th January 2006, 17:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.