Results 1 to 4 of 4

Thread: QWaitCondition woken by QFuture

  1. #1
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QWaitCondition woken by QFuture

    I am working on some producer/consumer code using QtConcurrent as the producer. The consumer is running in the main GUI thread and I would like it to do something whenever some work is ready. I can use a QWaitCondition and get the producer tasks to call QWaitCondition::wakeOne() when finished but am having trouble exiting the loop when the QFuture reports finished:

    Qt Code:
    1. Worker worker;
    2. QFuture future=QtConcurrent::map(start, end, worker);
    3.  
    4. QMutex mutex;
    5. QWaitCondition waitCondition;
    6. mutex.lock();
    7. while (true) {
    8. if (results.ready())
    9. processResults();
    10.  
    11. if (future.isFinished()) {
    12. mutex.unlock();
    13. break;
    14. else if (waitCondition.wait(&mutex, timeout) {
    15. break;//give up
    16. }
    17. }
    18.  
    19. Worker::operator() (int i) {
    20. doWork(i);
    21. waitCondition.wakeOne();
    22. }
    To copy to clipboard, switch view to plain text mode 

    The trouble is the QFuture may finish between the calls to isFinished() and wait() resulting in a delay of 'timeout'. I would like the QFuture finishing to do a wakeOne() and have tried unsuccessfully using a direct connection to QFutureWatcher::finished(). I think the QFutureWatcher may depend on the event loop and as the consumer is in the main thread, it isn't received.

    Would adding something like this be useful Qt enhancement to facilitate this:
    Qt Code:
    1. QFuture::addFinishedCondition(QWaitCondition wc, bool wakeAll);
    2. QFuture::addResultsReadyCondition(QWaitCondition wc, bool wakeAll);
    To copy to clipboard, switch view to plain text mode 

    Is there a solution using existing code?

    thanks.

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

    Default Re: QWaitCondition woken by QFuture

    seems like you could be using qfuturewatcher and void resultReadyAt ( int index )

    Isn't your loop backwards as well? ie the wait should be at the top of the loop.

    Also, I'm not sure qwaitcondition and qfuture are meant to go together how you are using. when you call wakeOne, the result for the future is not ready - the function hasnt completed. you are using wakeOne to do exactly what futurewatcher resultReadyAt ( int index ) is meant to do.


    There is a solution using existing code - use the signals from futurewatcher. dont use a loop in the main thread that blocks qt from processing events.
    Last edited by amleto; 23rd December 2011 at 18:21.
    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.

  3. #3
    Join Date
    Nov 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWaitCondition woken by QFuture

    use the signals from futurewatcher. dont use a loop in the main thread that blocks qt from processing events
    The reason the consumer is in the main thread is that it is performing rendering.

    when you call wakeOne, the result for the future is not ready - the function hasnt completed
    The function hasn't completed and the future doesn't know it has finished modifying data yet but I do. It would be better if the future could do the waking as proposed though.

    It seems QtConcurrent is a good high level threading module and Qt also provides good lower level code such as QWaitCondition and QMutex, but there is no bridge between the two. I think this may be an unnecessary limitation and providing a mechanism for QFuture to wake QWaitCondition would be a natural extension.

    Unfortunately I don't see anyway to change this without modifying Qt private code.

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

    Default Re: QWaitCondition woken by QFuture

    The reason the consumer is in the main thread is that it is performing rendering.
    The consumer can be perfectly happy in the main thread WITHOUT a blocking loop.

    It would be better if the future could do the waking as proposed though.
    the future would do the waking. if only you would let it.

    I don't think any bridge is necessary, and there definitely isn't any limitation imposed here.
    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.

Similar Threads

  1. QFutureWatcher finished without even starting a QFuture...
    By Zweistein in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2011, 14:30
  2. Appropriate QFuture use?
    By mgb_qt in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2010, 19:34
  3. QtConcurrent mappedReduced QFuture Core Dump
    By shawno in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2010, 02:29
  4. Replies: 14
    Last Post: 8th September 2009, 11:01
  5. QWaitCondition
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 16:15

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.