Results 1 to 6 of 6

Thread: QtConcurrent: clarifications needed

  1. #1
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QtConcurrent: clarifications needed

    I've followed this example: http://codejourneys.blogspot.it/2008...of-use-of.html that shows how to use QtConcurrent to execute a function in a secondary thread.

    Googling around, I've read somewhere that the QtConcurrent class would actually "balance" and span the workload on all available cores automatically. But after trying and reading some more, I think I understood that it's not really the case.
    QtConcurrent::run() will actually run a function in a secondary thread, and you have to use it multiple times to enable more threads. Is this correct?

    If so, say you have a myFunction performing n square roots, would you do something like this:

    Qt Code:
    1. for (int i=0; i< QThreadPool::globalInstance()->maxThreadCount(); i++)
    2. {
    3. itemsPerThread=n/QThreadPool::globalInstance()->maxThreadCount();
    4. itemStart=i*itemsPerThread;
    5. itemEnd=(i+1)*itemsPerThread;
    6. QtConcurrent::run(myFunction(itemStart,itemEnd));
    7. }
    To copy to clipboard, switch view to plain text mode 

    i.e., assigning a range of values to process for each thread?

    Thanks for any help.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtConcurrent: clarifications needed

    As far as my understanding goes, a QThreadPool acquires as many threads as possible with the given infrastructure. As and when the functions are called to be executed in a different thread, it is provided. Once the pool gets filled, the functions are essentially serialized(behaviour akin to single threaded in a way). What I like about QtConcurrent is it very easier to program with it. For every function I need to run in a thread, i need not have a separate QThread class for it. It is closer to the behaviour of c# and java. And it was a very easy switch for me.
    Your question seems to be of a Single Instruction multiple data. For each data in the container, the function will be called in a separate thread if possible. If not possible, some of them may be queued. I think of it as fire and forget system. And is quite efficient.

  3. #3
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtConcurrent: clarifications needed

    Thanks for the answer, but I still don't understand: is QtConcurrent supposed to automatically span over multiple threads, or do I have to setup n QtConcurrent threads?

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtConcurrent: clarifications needed

    Nope you don't have to do anything. It will be done for you.

  5. #5
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtConcurrent: clarifications needed

    QtConcurrent uses the QApplication global threadpool which automatically queries the number of logical cores.

    QThreadPool::globalInstance()->setMaxThreadCount(100) will let your QtConcurrent::run functions over allocate the number of threads which can execute concurrently (with thread context switches that is).

    I had a program which needed to spawn 100 or so QtConcurrents which do different things (not iterating over a map but dynamically invoking objects as an aggregate task) each thread semi-depended on the completion of another so I needed to have all the threads start at once and block until signalled. Maybe this is closer to the functionality you need.

  6. #6
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtConcurrent: clarifications needed

    Thank you very much for the suggestions, I now got QtConcurrent to work and use all the cores available.
    I suppose I need to use QFuture and QFutureWatcher to eventually check if threads have finished, and to manually stop those?

    Also, I have a Mac Pro dual processor: QtConcurrent will use all the cores from the first CPU only, while the second is ignored. Is perhaps a Qt bug (or, better, a missing feature) ?

Similar Threads

  1. Problem with QtConcurrent::run
    By januszmk in forum Newbie
    Replies: 5
    Last Post: 30th July 2011, 17:57
  2. QtConcurrent and Symbian
    By karlkar in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 30th June 2011, 13:48
  3. qtconcurrent
    By knishaq in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2009, 08:41
  4. QtConcurrent question
    By bair in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 10:46
  5. QtConcurrent
    By Brandybuck in forum An Introduction to QThreads
    Replies: 2
    Last Post: 9th May 2008, 14:10

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.