Results 1 to 6 of 6

Thread: Thread start overhead

Hybrid View

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

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,333
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    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.

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

    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, 16:12
  2. Qt5 and ICU - too much overhead?
    By sfcheng77 in forum Installation and Deployment
    Replies: 4
    Last Post: 2nd September 2013, 15:47
  3. Start a thread automatic at start
    By ralphot in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2013, 15:54
  4. thread->start() does not work ?
    By ErrMania in forum Newbie
    Replies: 3
    Last Post: 20th November 2011, 11:08
  5. How to start a thread (inline)
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 21: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
  •  
Qt is a trademark of The Qt Company.