Results 1 to 4 of 4

Thread: Launching QThreads from a loop

  1. #1
    Join Date
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Launching QThreads from a loop

    Hello,

    I have an algorithm which runs on image data, which I have implemented so that it occurs on a separate QThread to prevent locking the GUI. This works perfectly when I am running it with one image. Instead, I'd like to run a loop over multiple images, but only one thread executing at a time... something along the lines of...

    Qt Code:
    1. // Loop over all files.
    2. int i = 0;
    3. while (i < selected_files.size()) {
    4.  
    5. QThread* thread = new QThread;
    6. Worker *worker = new Worker(filenames,s2);
    7. worker->moveToThread(thread);
    8. connect(thread, SIGNAL(started()), worker, SLOT(process()));
    9. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    10. thread->start();
    11.  
    12. i+=step;
    13. }
    To copy to clipboard, switch view to plain text mode 

    But it currently runs all the threads at the same time. I want this to operate one thread at a time. I know this is probably very basic, but I am still learning threaded programming!

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Launching QThreads from a loop

    Then only start one thread that processes a queue of files rather than one thread for each entry in the queue.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Launching QThreads from a loop

    Qt Code:
    1. someclass::slot_make_next_thread()
    2. {
    3. // need some saved variables so we know which 'filenames' and which 's2' to pass on.
    4. make_thread(filenames, s2);
    5. }
    6.  
    7. someclass::make_thread(filenames, s2)
    8. {
    9. QThread* thread = new QThread;
    10. Worker *worker = new Worker(filenames,s2);
    11. worker->moveToThread(thread);
    12. connect(thread, SIGNAL(started()), worker, SLOT(process()));
    13. connect(thread, SIGNAL(finished()), this, SLOT(slot_make_next_thread()));
    14. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    15. thread->start();
    16. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  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: Launching QThreads from a loop

    Another solution:

    Qt Code:
    1. QImage algorithm(QImage img) {
    2. // ...
    3. }
    4.  
    5. QList<QImage> imagesToProcess = ...;
    6. QThreadPool::globalInstance()->setMaxThreadCount(1); // if you want to force to have only one thread at a time
    7. QFuture<QImage> future = QtConcurrent::mapped(imagesToProcess, algorithm);
    8. QFutureWatcher<QImage> *watcher = new QFutureWatcher<QImage>(this);
    9. watcher->setFuture(future);
    10. connect(watcher, SIGNAL(finished()), ..., ...);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  3. Launching problem
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 29th October 2007, 11:18
  4. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12
  5. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 09:41

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.