Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. does Qt have a thread-safe Queue?

does Qt have a thread-safe Queue?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.1k Views
  • 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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    does Qt have a thread-safe Queue?
    thanks

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      unless something has changed in recent versions you need to manage queue access using a semaphore.

      1 Reply Last reply
      2
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3
        #pragma once
        
        #include <queue>
        #include <mutex>
        #include <condition_variable>
        
            template <typename T>
            class SharedQueue
            {
            public:
                SharedQueue();
                ~SharedQueue();
        
                T& front();
                void pop_front();
        
                void push_back(const T& item);
                void push_back(T&& item);
        
                int size();
                bool empty();
        
            private:
                std::deque<T> queue_;
                std::mutex mutex_;
                std::condition_variable cond_;
            }; 
        
            template <typename T>
            SharedQueue<T>::SharedQueue(){}
        
            template <typename T>
            SharedQueue<T>::~SharedQueue(){}
        
            template <typename T>
            T& SharedQueue<T>::front()
            {
                std::unique_lock<std::mutex> mlock(mutex_);
                while (queue_.empty())
                {
                    cond_.wait(mlock);
                }
                return queue_.front();
            }
        
            template <typename T>
            void SharedQueue<T>::pop_front()
            {
                std::unique_lock<std::mutex> mlock(mutex_);
                while (queue_.empty())
                {
                    cond_.wait(mlock);
                }
                queue_.pop_front();
            }     
        
            template <typename T>
            void SharedQueue<T>::push_back(const T& item)
            {
                std::unique_lock<std::mutex> mlock(mutex_);
                queue_.push_back(item);
                mlock.unlock();     // unlock before notificiation to minimize mutex con
                cond_.notify_one(); // notify one waiting thread
        
            }
        
            template <typename T>
            void SharedQueue<T>::push_back(T&& item)
            {
                std::unique_lock<std::mutex> mlock(mutex_);
                queue_.push_back(std::move(item));
                mlock.unlock();     // unlock before notificiation to minimize mutex con
                cond_.notify_one(); // notify one waiting thread
        
            }
        
            template <typename T>
            int SharedQueue<T>::size()
            {
                std::unique_lock<std::mutex> mlock(mutex_);
                int size = queue_.size();
                mlock.unlock();
                return size;
            }
        
        1 Reply Last reply
        0
        • O Offline
          O Offline
          opengpu
          wrote on last edited by
          #4

          found this one at stackoverflow

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @opengpu said in does Qt have a thread-safe Queue?:

            found this one at stackoverflow

            Then you should also provide the link to the stack overflow post that has that code in it.

            Just in case, Qt also provides all the building blocks to implement a shared queue in a similar way.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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