Results 1 to 13 of 13

Thread: How to loop multithreaded job

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to loop multithreaded job

    Hi all!

    My base classes are:
    Qt Code:
    1. class Manager: public QThread
    2. class Worker: public QThread
    To copy to clipboard, switch view to plain text mode 

    Manager starts 4 (independed) workers and "listens" for their signals (and counts finished workers). Manager restarts workers several times.
    It also emits final results to GUI thread.
    This is all working flawless.

    Now, i want to loop whole job (manager's job) in Tester thread.

    Qt Code:
    1. void Tester::run(void)
    2. {
    3. for (int iParam1 = 0; iParam1 < parameter1.size(); iParam1++)
    4. {
    5. for (int iParam2 = 0; iParam2 < parameter2.size(); iParam2++)
    6. {
    7. double tmpValueSum = 0.0;
    8. for (int iRun = 0; iRun < numberOfRuns; iRun++)
    9. {
    10. manager->parameter1 = parameter1[iParam1];
    11. manager->parameter2 = parameter2[iParam2];
    12.  
    13. manager->run();
    14. manager->wait(); // This is a problem
    15. }
    16. results[iParam1][iParam2] = tmpValueSum / double(numberOfRuns);
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    I miss something in the way I understood wait function. I tried exit(), quit() and exec() in Manager...
    What modifications should be implemented to make this possible?

    In base case, i only have manager->run(); and there is no need for waiting... Manager emits some output and final results to GUI thread.

    Thanks!

    EDIT: I found this example
    .
    As I see it, the only important difference is that my thread raises other threads and have few connections...
    Last edited by stefan; 20th December 2012 at 16:23.

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

    Default Re: How to loop multithreaded job

    Calling run() does not start a new thread.
    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.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to loop multithreaded job

    As wysota said, QThread::run() is the method that you would like to have executed by the new thread. If you call it directly, obviously your current thread will execute it instead, in your case your test program.

    What you actually want to do is start the manager so its run() method is executed by a new thread.

    Qt Code:
    1. manager->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Default Re: How to loop multithreaded job

    ...which leads to suspecting the OP never had 4 threads running there at all.
    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.


  5. #5
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to loop multithreaded job

    Quote Originally Posted by anda_skoa View Post
    What you actually want to do is start the manager so its run() method is executed by a new thread.

    Qt Code:
    1. manager->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    ough... I know that. It's a typo... this could be the cause of all problems. I'll try it later.
    Sorry, i really didn't see it...

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. multithreaded QImageReader
    By dcole in forum Qt Programming
    Replies: 7
    Last Post: 23rd June 2011, 22:16
  3. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  4. Multithreaded Server
    By niol1000 in forum Newbie
    Replies: 1
    Last Post: 24th February 2010, 17:33
  5. Multithreaded spend CPU 100%
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2008, 07:03

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.