Results 1 to 8 of 8

Thread: ThreadPool ?

  1. #1
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default ThreadPool ?

    Who can that will prompt as to organize ThreadPool? I shall be glad to examples. It is necessary to set the maximal size of a pool, to operate streams (start, end of a stream)

  2. #2
    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: ThreadPool ?

    I don't entirely understand what you want, but if you want to have a thread pool, you'll need a wait condition that will start or stop threads from within a pool and an array of thread objects. If you want something more advanced, a controller spawning and destroying threads should be implemented as well. Trying QtConcurrent (available at labs.trolltech.com) might be a good idea as well.

  3. #3
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: ThreadPool ?

    I need to store the list of the started processes (or them ID). During work I should have an opportunity at any moment to finish process.

  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: ThreadPool ?

    Processes or threads? Are you using QProcess or QThread to spawn them? Both of these classes allow you to terminate the entity they control.

  5. #5
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: ThreadPool ?

    Quote Originally Posted by wysota View Post
    Processes or threads? Are you using QProcess or QThread to spawn them? Both of these classes allow you to terminate the entity they control.
    QThread !
    I think to use QList for this purpose:
    create :
    Qt Code:
    1. QList<CWorkThread*> threadList;
    2.  
    3. if( threadList.size() < 4)
    4. {
    5. CWorkThread *thread = new CWorkThread(this);
    6. threadList.append(thread);
    7. thread->run();
    8. }
    To copy to clipboard, switch view to plain text mode 

    and for terminate:

    Qt Code:
    1. for(int i = 0; i < threadList.size(); i++)
    2. {
    3. CWorkThread *nb = threadList.at(i);
    4. nb->terminate();
    5. nb->wait();
    6. threadList.erase(threadList.begin()+i);
    7. i--;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Whether there will be it the correct decision???

  6. #6
    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: ThreadPool ?

    First of all you should never use terminate() unless you know what you are doing. Second of all I don't see any "pool" here, just a bunch of threads. I think you should just let all threads do their job and connect to each thread's finished() signal and delete the thread when it's done doing whatever it was doing.

  7. #7
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: ThreadPool ?

    I shall think above that that you have told. But in me can be caused 4-5 threads for copying files in the size 10-80 Gb, and there should be an opportunity to stop copying. There are other ways of the decision?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ThreadPool ?

    Yes, you could tell the thread to stop.
    The most common way is to add a volatile boolean flag in the thread and a setter.
    When you set this flag to true(from outside the thread) it means the thread should stop. You have to test for this flag in the thread in all extensive operations.
    For example, if you copy the files in a loop, then you should test that flag and break the loop when it is false.

    Regards

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.