Results 1 to 19 of 19

Thread: Advise sought on QEventLoop behavior

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    62
    Thanked 4 Times in 4 Posts

    Default Re: Advise sought on QEventLoop behavior

    Per Wysota's example of the QConcurrent-based solution I am now looking at QFuture usage. I don't understand how to implement following scenario using this framework.

    My application processes various data files and submits it to the website for storage. Each data file comes from a separate provider. So, in respect to QConcurrent, I can describe operation for data processing using functor that I can pass to QtConcurrent::map. But this instance has to make multiple requests to the website via webservice, wait for the response and then decide what to do next - either stop or continue to send data until it finishes. If more data is available it makes another request to send data and so on.

    So, within this function object I have to implement waiting mechanism. Is my scenario a good candidate for QConcurrent or I should stick with direct threads?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Advise sought on QEventLoop behavior

    Why do you want threads there at all? You can do all that in a single 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
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    62
    Thanked 4 Times in 4 Posts

    Default Re: Advise sought on QEventLoop behavior

    There is more to the use case that make threading a right choice. What I don't understand is if QtConcurrent provieds support beyond the sequential tasks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Advise sought on QEventLoop behavior

    Quote Originally Posted by TorAn View Post
    There is more to the use case that make threading a right choice.
    Name one thing, please.
    What I don't understand is if QtConcurrent provieds support beyond the sequential tasks.
    QtConcurrent doesn't care about the nature of tasks it executes.
    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
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    62
    Thanked 4 Times in 4 Posts

    Default Re: Advise sought on QEventLoop behavior

    For example, data acquisition processes for individual datasources can take from 3 min to 2 hours, depending on the datasource. There around 10 datasources that have to be processed, starting at the same time.

    I understand that QtConcurrent can process any task. But when the task that is processed is event or signal driven I have to organize it in the task instance and that brings back eventloop into the task.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Advise sought on QEventLoop behavior

    Quote Originally Posted by TorAn View Post
    For example, data acquisition processes for individual datasources can take from 3 min to 2 hours, depending on the datasource. There around 10 datasources that have to be processed, starting at the same time.
    So why is it useful to do it in threads? All your threads will be doing most of the time is waiting for data to flow in. One thread is enough to handle all that without any effort.

    I understand that QtConcurrent can process any task. But when the task that is processed is event or signal driven I have to organize it in the task instance and that brings back eventloop into the task.
    Obviously, if you need events, you need an event loop. But you don't need an event loop for signals.

    If I understand correctly what you are trying to do, then you are way overcomplicating things. The best strategy is to acquire the data from network (or whatever outside source you're using) in a single thread for all the sources and then process the data using QtConcurrent. This gives you the simplest and the most efficient code.
    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.


  7. #7
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    62
    Thanked 4 Times in 4 Posts

    Default Re: Advise sought on QEventLoop behavior

    Wysota,

    I very much appreciate your review. I agree with the approach you suggested, part of it is actually implemented in the production code. Network communication is shared between threads, only data processing happends individually in a separate threads. All the reasons to keep it that way are relatively weak and I should redo the implementation using one thread for network and another for data processing.

    Per the results of my use case sample I already made the transition from QRunnable/QEventLoop to QThread. It required a miniscule change and everything works now as expected. However, in the next round of development I will make the next transition, from separate QThreads to one QThread and QConcurrent maps.

    Your advise and support is very helpful, thanks again.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Advise sought on QEventLoop behavior

    Quote Originally Posted by TorAn View Post
    However, in the next round of development I will make the next transition, from separate QThreads to one QThread and QConcurrent maps.
    No, no QThread. The main thread will do just fine, you don't need an extra thread for networking.
    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.


  9. The following user says thank you to wysota for this useful post:

    TorAn (9th June 2011)

Similar Threads

  1. Advise sought on working with binary files across OSes
    By TorAn in forum General Programming
    Replies: 2
    Last Post: 19th September 2010, 07:44
  2. Replies: 4
    Last Post: 1st June 2010, 12:42
  3. static QEventLoop strange behavior
    By SABROG in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 13:04
  4. advise sought on TreeView model design
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 25th June 2009, 15:36
  5. Replies: 6
    Last Post: 25th February 2008, 22:18

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.