Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtConcurrent using Sequence generated on-the-fly



  • Documentation and all examples I've seen assume that "Sequence ":http://qt-project.org/doc/qt-5/qtconcurrent.html#mapped (e.g. QList, QVector, etc.) is given in advance. That is, we know how many mapFunction() calls (and its' parameters) there will be beforehead.

    Problem I am faced with is that new items that need to be proceded might emerge during the execution of QtConcurrent::mapped(). How can it be acheived?

    I had an idea to inherit QFuture::const_iterator, override operators and use QtConcurrent::mapped(ConstIterator, ConstIterator, MapFunction) with ConstIterator being inherited class. Is it a good approach?


  • Moderators

    Hi,

    [quote author="AlexPo" date="1389308650"]Documentation and all examples I've seen assume that "Sequence ":http://qt-project.org/doc/qt-5/qtconcurrent.html#mapped (e.g. QList, QVector, etc.) is given in advance. That is, we know how many mapFunction() calls (and its' parameters) there will be in advance.[/quote]Yes, that is a fundamental assumption of the map/filter/reduce functions. Your container is expected to remain the same size at all times.

    [quote]Problem I am faced with is that new items that need to be proceed might emerge *during * the execution of QtConcurrent::mapped(). How can it beforehead?[/quote]Would it be possible to do your processing in batches?

    i.e. Call mapped() for a fixed-sized container. If any new items emerge, collect them in a new container. Then, run mapped() again. Repeat until there are no more new items.

    [quote]I had an idea to inherit QFuture::const_iterator, override operators and use QtConcurrent::mapped(ConstIterator, ConstIterator, MapFunction) with ConstIterator being inherited class. Is it a good approach?[/quote]I wouldn't. For one thing, you will chang the meaning of const, and break the assumptions that Qt Concurrent makes.

    Another possible approach is to subclass QRunnable instead. Create a QRunnable for each item to be processed. You can add new QRunnables to the thread pool on-the-fly. You can collect results by adding them to a mutex-protected shared container.



  • Thanks for reply!

    [quote author="JKSH" date="1389316006"]Would it be possible to do your processing in batches?

    i.e. Call mapped() for a fixed-sized container. If any new items emerge, collect them in a new container. Then, run mapped() again. Repeat until there are no more new items.
    [/quote]

    Yes, it would. But in my case that would be rather ugly design.

    [quote author="JKSH" date="1389316006"]I wouldn't. For one thing, you will chang the meaning of const, and break the assumptions that Qt Concurrent makes.

    Another possible approach is to subclass QRunnable instead. Create a QRunnable for each item to be processed. You can add new QRunnables to the thread pool on-the-fly. You can collect results by adding them to a mutex-protected shared container.[/quote]

    I see. QRunnable then. Thanks again!


Log in to reply