Results 1 to 6 of 6

Thread: Thread start overhead

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Thread start overhead

    Hi,

    I'm developing an application that have many threads. These threads were started and then it waits until all threads have finished the work.
    I start all the threads using a loop and I have calculated that the time spent to start these threads is high. High means more ore less 25ms to start 18 threads. It seems not to much but this is my bootleneck.

    My Idea is to use a QWaitCondition on every thread. Start the threads. As they do not have data to process will go to sleep waiting a signal to the wait condition.
    When I need them to process I can cal "wakeOne" to let the thread go up an process. After it finish the process it will go to sleep again.

    Could this work?

    And, if this works, how can I wait that all threads have finished the work?

    Note: There is a worker thread(do not have an event loop) that start all the other threads.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Dec 2017
    Location
    Turkey
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread start overhead

    How do you measure thread initialization time?

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread start overhead

    Quote Originally Posted by ^NyAw^ View Post
    And, if this works, how can I wait that all threads have finished the work?
    Make a list with pointers to threads. After finishing worker sends a signal. In slot remove the worker from the list. If list empty done.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread start overhead

    Hi,

    Quote Originally Posted by son
    How do you measure thread initialization time?
    Using QElapsedTimer

    Quote Originally Posted by Lesiok View Post
    Make a list with pointers to threads. After finishing worker sends a signal. In slot remove the worker from the list. If list empty done.
    Think on that the worker thread is a thread with a loop that starts all the other threads, so there is no event loop in this worker thread.

    This is my approach
    Qt Code:
    1. MyThread::run()
    2. {
    3. for (int i=0; i<qNumThreads; i++)
    4. m_qThreadList[i]->start();
    5.  
    6. for (int i=0; i<qNumThreads; i++)
    7. m_qThreadList[i]->wait();
    8.  
    9. //Get results
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Òscar Llarch i Galán

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,783
    Thanks
    262
    Thanked 773 Times in 763 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Thread start overhead

    m_qThreadList[i]->wait();
    Hmmm, isn't this a blocking function? If wait() doesn't return until the thread has finished, then your loop will not complete until all threads have finished, and MyThread::run() won't return either.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread start overhead

    Hi,

    Let me be a little more presice:
    Qt Code:
    1. MyThread::run()
    2. {
    3. while(!bExit)
    4. {
    5. get_image_from_camera
    6. for (int i=0; i<qNumThreads; i++)
    7. m_qThreadList[i]->start();
    8.  
    9. for (int i=0; i<qNumThreads; i++)
    10. m_qThreadList[i]->wait();
    11.  
    12. //Get results and emit some signal to let the GUI show results
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    "MyThread" is running in a loop until the user stops it.
    The other Threads(ThreadList) are processing Threads that compute some image processing algorithms parallel, so I need to start them and wait for them to finish to get results.

    The overhead seems to be on the thread start. The OS have to create a thread, stack, copy system variables, ...

    So, my question was if I could start the threads but put them to sleep with a QWaitCondition. When the "MyThread" have captured a frame from the camera, set a boolean let the threads process it. Finally what I don't know how to wait until the threads have finished.

    Something like this wastes CPU on MyThread
    Qt Code:
    1. for (int i=0; i<qNumThreads; i++)
    2. {
    3. while (m_qThreadList[i]->isProcessing())
    4. ;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Maybe using another QWaitCondition that the threads could call "wakeOne()" ?
    Qt Code:
    1. for (int i=0; i<qNumThreads; i++)
    2. {
    3. m_qThreadList[i]->m_qWaitCondition->wait();
    4. //Here we know that thread "i" have finished its process
    5. }
    To copy to clipboard, switch view to plain text mode 

    Could this work?

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 1
    Last Post: 6th December 2015, 17:12
  2. Qt5 and ICU - too much overhead?
    By sfcheng77 in forum Installation and Deployment
    Replies: 4
    Last Post: 2nd September 2013, 16:47
  3. Start a thread automatic at start
    By ralphot in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2013, 16:54
  4. thread->start() does not work ?
    By ErrMania in forum Newbie
    Replies: 3
    Last Post: 20th November 2011, 12:08
  5. How to start a thread (inline)
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 22:53

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.