Results 1 to 8 of 8

Thread: Speed up Producer Consumer Threads

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Speed up Producer Consumer Threads

    Hi,

    I have two threads in my application. One is a Producer and the other one is a Consumer. The proucer does some work and create a own class object that is inserted on a ring buffer. The consumer reads this buffer objects(when there are almost one).
    The consumer then emits the consumed object to the main thread to let it display some data of this object, but it is done by value(not by reference or pointer). This is done because if I pass the object by reference or pointer to the main thread, maybe when it goes to read it, the consumer thread is modifying it and then the values are incorrect.

    I want to speed up my application and my question is to use a QMutex ring buffer wich will lock the object so I could pass it as reference(without spending the time to copy the object), and when the main thread finish the reading process it will unlock it to let the consumer and producer continue.

    Have anyone a good idea how to speed up this process?

    Thanks,
    Òscar Llarch i Galán

  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: Speed up Producer Consumer Threads

    Hmm... mutexes slow down applications, not speed them up...

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Speed up Producer Consumer Threads

    Hi,

    Hmm... mutexes slow down applications, not speed them up...
    This is another question... I use a ring buffer with QWaitConditions to lock the threads but I was thinking on use a QQueue without mutexes or waitConditions, so the producer queues objects and the consumer dequeues objects. But the problem will be if the producer is faster than the consumer.

    Thanks,
    Òscar Llarch i Galán

  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: Speed up Producer Consumer Threads

    wait conditions use mutexes so... you are exchanging mutexes for mutexes... I suggest you use custom events to transfer data to the main thread and let some object there handle them. This way you won't be copying whole objects, just the data you really need. Custom events are thread safe, so you won't need any additional synchronisation there.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Speed up Producer Consumer Threads

    Hi,

    I have not readed about custom events, is there a big difference to use signals and slots?
    Òscar Llarch i Galán

  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: Speed up Producer Consumer Threads

    Actually signals across threads are delivered using events, so you may use signals if you want. The whole point is to transfer only the data, not the whole object. If you really have to transfer the whole object, make it implicitly shared, it will then be very lightweight. Read about QSharedData and QSharedDataPointer to learn how to implement implicitly shared classes.

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

    ^NyAw^ (29th February 2008)

  8. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Speed up Producer Consumer Threads

    Hi,

    I have forget to tell that the objects that I'm passing thorugh signals and slots are from closed source lib and I have not access to them. So maybe creating my own class derived from QSharedData and inserting a object reference(from the closed source lib) into it will be possible.
    Then, this object could be shared. Right?

    Thanks,
    Òscar Llarch i Galán

  9. #8
    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: Speed up Producer Consumer Threads

    No, there is no point in doing that and it would not work anyway. In that case I suggest you extract all data you need from the object, send the data to the other thread and forget about the object. Another possibility is to consume in the GUI thread - then you'll be able to draw immediately.

Similar Threads

  1. Producer Consumer...
    By Shuchi Agrawal in forum Newbie
    Replies: 1
    Last Post: 16th February 2007, 09:45
  2. Producer Consumer
    By ^NyAw^ in forum Qt Programming
    Replies: 16
    Last Post: 17th November 2006, 19:53

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.