Results 1 to 2 of 2

Thread: An OO consumer/producer design?

  1. #1
    Join Date
    Jun 2006
    Posts
    43
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default An OO consumer/producer design?

    All the Example code in the Threading section are coded in the older style of subclassing QThread. One of the problems I see with this is that every consumer then spawns its own thread.

    I have a slow system and have a need for a "best effort" object on a thread. i.e. A consumer that keeps up as best it can with small inputs that come in too fast. If it's busy and 2+ more inputs come in it only processes the latest one on the next iteration. My previous implementation was a loop in the QThread::run() function with a mutex protected input block and a binary semaphore to control the loop. When not in use, the thread merely locks the loop and waits.

    As stated above, this has the limitation that every consumer must spawn its own thread. I was hoping to modify this into multiple producers connected to multiple consumers, which could be moved to a generic thread context using moveToThread().

    The problem: This architecture provides no concept of "skipping" inputs that are coming too fast. The consumers residing on their own, shared thread rely on the thread's event loop. There's no look-ahead capability to see if a newer signal of that type is waiting in the queue. Has anyone tackled this issue? Ideas?

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

    Default Re: An OO consumer/producer design?

    What you have is not a classic producer-consumer situation. Have a mutex (and possibly a wait condition) protected variable where you assign the latest input. If the consumer doesn't manage to process it before the producer kicks in, the producer will overwrite content of the variable with new input and signal the consumer, there is work available. There is no benefit of having an event loop here so subclassing QThread and reimplementing run() is the proper way to go.
    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. The following user says thank you to wysota for this useful post:

    Mookie (13th April 2014)

Similar Threads

  1. Any Qt code/example for producer consumer pattern?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2009, 21:11
  2. Accessing deleted memory, Producer Consumer app
    By ^NyAw^ in forum General Programming
    Replies: 3
    Last Post: 2nd August 2008, 13:58
  3. Speed up Producer Consumer Threads
    By ^NyAw^ in forum Qt Programming
    Replies: 7
    Last Post: 29th February 2008, 18:38
  4. Producer Consumer...
    By Shuchi Agrawal in forum Newbie
    Replies: 1
    Last Post: 16th February 2007, 09:45
  5. Producer Consumer
    By ^NyAw^ in forum Qt Programming
    Replies: 16
    Last Post: 17th November 2006, 19:53

Tags for this Thread

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.