Results 1 to 2 of 2

Thread: How to properly synchronize two threads accessing a QQueue simultaneously

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to properly synchronize two threads accessing a QQueue simultaneously

    Hello,
    in my software design I need two threads to access a queue simultaneously. One of them pushes items at the end of the queue and the other one pop items from the same queue. I declared the queue as global. I have a rough idea of how to synchronize two threads accessing the same resource using QMutex but in this case the thread that feeds the queue does it practically almost continuously so how to find a timeslot for the second thread so that it can extract items from the queue.
    Can I have a help with the logic?
    Thanx
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to properly synchronize two threads accessing a QQueue simultaneously

    so how to find a timeslot for the second thread so that it can extract items from the queue.
    You don't have to "find a timeslot". Isolate the code that reads and writes from the queue into two separate methods and protect each of those methods internally with a QMutexLocker wrapping your QMutex instance (which should be a member of the same class as the access methods. The queue doesn't have to be global. It could be a member variable of the same class that holds the mutex and read / write methods for the queue. Give each thread a pointer to this manager class and they will call the read / write methods through that.

    The mutex and the OS will take care of granting access for each thread. If you find that the read thread isn't able to keep up with the write thread because the write thread is dominating access to the queue, then maybe consider giving the read thread a higher priority so it gets to run more frequently.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    franco.amato (4th January 2021)

Similar Threads

  1. Sharing Data Between two Threads - QQueue or Semaphores?
    By Knudsen09 in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2020, 21:21
  2. Replies: 1
    Last Post: 20th August 2018, 10:30
  3. Multiple threads accessing QList
    By Vikram.Saralaya in forum Qt Programming
    Replies: 7
    Last Post: 11th January 2016, 08:08
  4. synchronize threads
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2009, 08:08
  5. QT3: accessing singletons from within threads
    By karye in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2006, 12:05

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.