Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Producer–consumer multithread QQueue + QSemaphore
Qt 6.12 LTS is out! See what's new in the release blog

Producer–consumer multithread QQueue + QSemaphore

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drazz
    wrote on last edited by Drazz
    #1

    Hello,

    I've got two classes in different threads, consumer and producer that share one QQueue and one QSemaphore.

    Producer

    void Producer::run() {
        while(1) {
            if (dataReady) {
                queue.enqueue(data);
                semaphore.release();
            }
        }  
    }
    

    Consumer

    void Consumer::run() {
    
        while(1) {
            if (semaphore.acquire(1)) {
                auto data = queue.dequeue();
                doSomething();
            }
        }
    }
    

    I'm wondering if my queue data is thread safe or if I should subclass QQueue and use QSemaphore and QMutex to protect the shared resoruce because (https://doc.qt.io/qt-5/qtcore-threads-semaphores-example.html) uses two QSemaphore.

    Do I have to lock a mutex before enqueue/dequeue data and unlock it after? I yes, I don't understand the advantages of using QSemaphore here.

    kshegunovK 1 Reply Last reply
    0
    • D Drazz

      Hello,

      I've got two classes in different threads, consumer and producer that share one QQueue and one QSemaphore.

      Producer

      void Producer::run() {
          while(1) {
              if (dataReady) {
                  queue.enqueue(data);
                  semaphore.release();
              }
          }  
      }
      

      Consumer

      void Consumer::run() {
      
          while(1) {
              if (semaphore.acquire(1)) {
                  auto data = queue.dequeue();
                  doSomething();
              }
          }
      }
      

      I'm wondering if my queue data is thread safe or if I should subclass QQueue and use QSemaphore and QMutex to protect the shared resoruce because (https://doc.qt.io/qt-5/qtcore-threads-semaphores-example.html) uses two QSemaphore.

      Do I have to lock a mutex before enqueue/dequeue data and unlock it after? I yes, I don't understand the advantages of using QSemaphore here.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #3

      This is asked often, look here:

      https://forum.qt.io/topic/75880/proper-way-to-manage-information-between-threads
      https://forum.qt.io/topic/90127/blocking-queue-between-qthreads
      https://forum.qt.io/topic/116093/queue-enque-and-deque-thread-safe

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Since the documentation of QQueue does not mention that it's thread-safe you must assume that it isn't thread-safe.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        5
        • D Drazz

          Hello,

          I've got two classes in different threads, consumer and producer that share one QQueue and one QSemaphore.

          Producer

          void Producer::run() {
              while(1) {
                  if (dataReady) {
                      queue.enqueue(data);
                      semaphore.release();
                  }
              }  
          }
          

          Consumer

          void Consumer::run() {
          
              while(1) {
                  if (semaphore.acquire(1)) {
                      auto data = queue.dequeue();
                      doSomething();
                  }
              }
          }
          

          I'm wondering if my queue data is thread safe or if I should subclass QQueue and use QSemaphore and QMutex to protect the shared resoruce because (https://doc.qt.io/qt-5/qtcore-threads-semaphores-example.html) uses two QSemaphore.

          Do I have to lock a mutex before enqueue/dequeue data and unlock it after? I yes, I don't understand the advantages of using QSemaphore here.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #3

          This is asked often, look here:

          https://forum.qt.io/topic/75880/proper-way-to-manage-information-between-threads
          https://forum.qt.io/topic/90127/blocking-queue-between-qthreads
          https://forum.qt.io/topic/116093/queue-enque-and-deque-thread-safe

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          3
          • D Offline
            D Offline
            Drazz
            wrote on last edited by
            #4

            Thanks for the info. I was able to find my answer on this thread:

            @kshegunov said in Proper way to Manage Information Between Threads:

            @VRonin said in Proper way to Manage Information Between Threads:

            The semaphore is redundant here.

            No, it is not!!!

            @darkp03 said in Proper way to Manage Information Between Threads:

            I was quite disappointed to know that reimplementing the run method isnt the correct way, but everywhere on internet says that.

            Don't be, the internet is wrong. It's a valid thing to do if you don't need to use any of the signal-slot machinery. However your queue implementation looks a bit off. Consider this one:
            Producer(s):

            QByteArray data; //< Data to put in the queue
            
            inputNeeds.acquire();	// First wait for more data to be consumed if needed (do not overfill the queue).
            
            QMutexLocker lock(&queueMutex);
            queue.enqueue(data);
            queueAvailability.release();
            

            Consumer(s):

            queueAvailability.acquire();
            QMutexLocker lock(&queueMutex);
            if (queue.size() <= 0)
                return; //< So graceful exit is possible
            
            QByteArray data = queue.dequeue();
            inputNeeds.release();
            

            Where variables are:

            QSemaphore queueAvailability, inputNeeds; //< inputNeeds is initialized to some number (e.g. 10000) in the beginning - that's the maximum number of items the queue will hold so it doesn't overflow if the consumer(s) lag behind
            QMutex queueMutex;
            QQueue<QByteArray> queue;
            

            So mutex and sempahore makes sense

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt
            • Unsolved