Results 1 to 13 of 13

Thread: How to loop multithreaded job

  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...

  6. #6
    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

    Hi, it's me again

    I have a problem running my code on windows. It works on Linux!
    The probelm must be with threads...

    I suspect on this part of code:
    Qt Code:
    1. QVector<Pso*> psoInstances;
    2. for(int iThread=0; iThread<numberOfThreads; iThread++)
    3. {
    4. Pso* pso = initPsoInstance();
    5. psoInstances.push_back(pso);
    6.  
    7. // Dimension vs Swarm size
    8. int maxfuncalls = 100000;
    9. pso->dimensions = parameter1[iParam1];
    10. QVector<double> lb(pso->dimensions), ub(pso->dimensions);
    11. lb.fill(-50.0);
    12. ub.fill(50.0);
    13. pso->lowerBound = lb;
    14. pso->upperBound = ub;
    15. pso->psoParameters.swarmSize= parameter2[iParam2];
    16. pso->psoParameters.maxIterations = int(double(maxfuncalls)/double(pso->psoParameters.swarmSize));
    17.  
    18. pso->clear();
    19. pso->start();
    20. }
    21.  
    22. for(int iThread=0; iThread<numberOfThreads; iThread++)
    23. {
    24. psoInstances[iThread]->wait();
    25.  
    26. tmpValueSum += psoInstances[iThread]->iteration * psoInstances[iThread]->psoParameters.swarmSize;
    27.  
    28. delete psoInstances[iThread]->evaluator;
    29. delete psoInstances[iThread];
    30. }
    31. psoInstances.clear();
    To copy to clipboard, switch view to plain text mode 

    I'm running this in Visual Studio 2010.
    During the execution the following message is repeatedly outputted:
    Qt Code:
    1. The thread 'Swarm' (0xc74) has exited with code 0 (0x0).
    To copy to clipboard, switch view to plain text mode 

    When debugging the code it get stuck on wait() function in second loop.

    Is there any thread-related difference in Qt code behavior between Linux and Windows?

    Just to mention
    Qt Code:
    1. class Pso: public QThread
    To copy to clipboard, switch view to plain text mode 


    Any suggestion is more then welcome!

    Thanx


    Added after 22 minutes:


    I found problem!

    There is no problem with wait function. In Debug the code i too slow to do anything. It works in Release.
    Is this normal for debugging multithread calculations?
    It's completely uselss to do anything in Debug... I would wait days for result.
    Also, in Release there is no message
    Qt Code:
    1. The thread 'Swarm' (0xc74) has exited with code 0 (0x0).
    To copy to clipboard, switch view to plain text mode 
    Last edited by stefan; 7th January 2013 at 16:08.

  7. #7
    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

    I thing the problem is the thread exits before you call wait on it. In general you have a somewhat wrong approach. You should monitor each thread's finished() signal and when you receive it from all the threads, you should clean up your environment. There is no need for this "for" loop you have there. The fact that it works for you in Linux is probably either incidental or caused by different semantics of the wait() call. Either way this is not a good approach.
    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.


  8. #8
    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 wysota View Post
    I thing the problem is the thread exits before you call wait on it. In general you have a somewhat wrong approach. You should monitor each thread's finished() signal and when you receive it from all the threads, you should clean up your environment. There is no need for this "for" loop you have there. The fact that it works for you in Linux is probably either incidental or caused by different semantics of the wait() call. Either way this is not a good approach.
    Thanks wysota!

    The problem is that this code is in two external for loops. If I do this by implementing onPsoFinished slot (and connect it to Pso::finished() signal) then I cant have external for loops. I must do function which increments all 2 counters and starts new set of Pso threads when previous is finished. It looks messy.
    Why cant I use wait function in this way? What is its purpose if not waiting for thread to exit?

  9. #9
    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

    Quote Originally Posted by stefan View Post
    The problem is that this code is in two external for loops. If I do this by implementing onPsoFinished slot (and connect it to Pso::finished() signal) then I cant have external for loops. I must do function which increments all 2 counters and starts new set of Pso threads when previous is finished. It looks messy.
    I don't know what you mean. I don't see any reason to have new set of threads each time.

    What is its purpose if not waiting for thread to exit?
    You are waiting for thread 1 to exit, then thread 2, then thread 3, then... What if thread 2 exits before thread 1? Then you're calling wait() on a thread that has already finished and end up with the message you're getting.
    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.


  10. #10
    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 wysota View Post
    I don't know what you mean. I don't see any reason to have new set of threads each time.
    Even if i dont create new threads i must "restart" them...

    Maybe this "pseudo code" would clear things...
    Qt Code:
    1. for p1=1:10
    2. for p2=1:20
    3. for run=1:25
    4. Pso.set(p1,p2)
    5. run 4 x Pso (in 4 threads)(each Pso results are random based!)
    6. calculate average of all runs (4x25=100)
    7. results[p1][p2] = average
    To copy to clipboard, switch view to plain text mode 

    The best way would be to make threading on external loop (one thread for each p1) but Pso execution time is greatly depended on p1 and p2.
    That is why I made threading inside of "runs" loop.

    I know how to do it with finished() signal - but code dont look so clean

    Quote Originally Posted by wysota View Post
    You are waiting for thread 1 to exit, then thread 2, then thread 3, then... What if thread 2 exits before thread 1? Then you're calling wait() on a thread that has already finished and end up with the message you're getting.
    I see... That make sense!
    Waiting for allready finished thread is a bad thing to do?
    Qt Doc:
    This function will return true if the thread has finished. It also returns true if the thread has not been started yet.

  11. #11
    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

    Quote Originally Posted by stefan View Post
    Waiting for allready finished thread is a bad thing to do?
    No idea, apparently it doesn't work for you.

    I would approach the problem from a completely different end. I'd probably have a single object containing all information about the whole job and make it manage everything.
    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.


  12. #12
    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

    One way to wait for a set of workers to complete their work is to use a QSemaphore.

    Each worker thread acquires one unit of the semaphore when it enters its run() method and releases one unit before it exits.
    The main thread releases one unit whenever it starts a thread (thus allowing the thread to acquire it).
    When the main thread wants to wait for all to be finished it acquires as many units as it created threads. if all threads have exited already, this will return immediately. if any thread is still running, the acquire(numThreads) will block until the last worker has release the unit it was holding.

    Cheers,
    _

  13. #13
    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

    Actually with a semaphore you can omit the step of acquiring units by the threads -- simpy initialize the semaphore with the value of 0. The advantage is that there is no race condition if some worker threads are started with a delay larger than it takes the main thread to try and acquire the semaphore.
    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. 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.