Implement thread-safe queue in C++ Last Updated : 14 Sep, 2024 Comments Improve Suggest changes Like Article Like Report What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchronized, as the internal structure of the queue makes sure that the threads do not interfere with each other. Thus, it provides a safe and efficient way to access shared resources from multiple threads.Why should we use Thread-safe Queue?Thread-safe queues are commonly used in multi-threaded applications, where multiple threads need to access a shared resource. By using a thread-safe queue, the threads can safely access the queue without the need for synchronization. This makes it more efficient and less prone to errors. Thread-safe queues are also useful for tasks that need to be performed in parallel, as they can be used to distribute tasks among multiple threads.Implementation:A thread-safe queue in C++ can be implemented using a mutex along with a standard queue. A mutex is a synchronization object used to protect access to a shared resource, such as a thread-safe queue. The mutex should be locked before pushing or popping elements from the queue and unlocked after the operation is complete. A mutex is used to protect access to the queue, while a condition variable is used to wait for changes to the queue.First, the mutex is used to lock the queue whenever a thread attempts to access it. This ensures that only one thread can access the queue at a time. After the thread has finished accessing the queue, it can unlock the mutex. Next, a condition variable is used to wait for changes to the queue. When a thread adds an item to the queue, it signals the condition variable to indicate that the queue has changed. This allows threads that are waiting for changes to the queue to be woken up and continue.Finally, when a thread attempts to remove an item from the queue, it must first check if the queue is empty. If it is, it can wait on the condition variable until an item is added to the queue. This ensures that the thread will not attempt to remove an item from an empty queue.Below is the implementation of the above discussion: C++ // C++ implementation of the above approach #include <condition_variable> #include <iostream> #include <mutex> #include <queue> // Thread-safe queue template <typename T> class TSQueue { private: // Underlying queue std::queue<T> m_queue; // mutex for thread synchronization std::mutex m_mutex; // Condition variable for signaling std::condition_variable m_cond; public: // Pushes an element to the queue void push(T item) { // Acquire lock std::unique_lock<std::mutex> lock(m_mutex); // Add item m_queue.push(item); // Notify one thread that // is waiting m_cond.notify_one(); } // Pops an element off the queue T pop() { // acquire lock std::unique_lock<std::mutex> lock(m_mutex); // wait until queue is not empty m_cond.wait(lock, [this]() { return !m_queue.empty(); }); // retrieve item T item = m_queue.front(); m_queue.pop(); // return item return item; } }; // Driver code int main() { TSQueue<int> q; // Push some data q.push(1); q.push(2); q.push(3); // Pop some data std::cout << q.pop() << std::endl; std::cout << q.pop() << std::endl; std::cout << q.pop() << std::endl; return 0; } Output1 2 3Time Complexity: Best Case : O(1), Worst Case: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us O ojasvigupta Follow Improve Article Tags : Queue Technical Scripter DSA Technical Scripter 2022 Practice Tags : Queue Similar Reads Queue in C++ STL In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat 4 min read How to Iterate a STL Queue in C++? A Queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Syntax: queue<datatype> queuename;Datatype: Queue can take any data type depending on the values, e.g. int, char, float, etc. The std: :queue container d 4 min read queue push() and pop() in C++ STL The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file.In this arti 2 min read queue::front() and queue::back() in C++ STL Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can 3 min read queue::empty() and queue::size() in C++ STL Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis 4 min read queue::emplace() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This fu 3 min read queue::swap() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() fun 2 min read Queue of Pairs in C++ STL with Examples Queue in STL are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement where elements are inserted at the back (end) and are deleted from the front. Queue of pair can be very efficient in designing complex data structures. The first element is referenced as âf 2 min read Queue using Stacks Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations.Table of ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making 11 min read Implement thread-safe queue in C++ What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr 3 min read Like