CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Thread safe deque implementation

  1. #1
    Join Date
    Dec 2018

    Thread safe deque implementation

    Im a newbie to c++.
    I need to implement deque with two threads. One thread will push back the data for every 1 sec and other thread will pop out the data from deque for every 0.5 secs.these two threads run in infinite loop
    Can anyone help me to solve this

  2. #2
    Join Date
    Feb 2017

    Re: Thread safe deque implementation

    Quote Originally Posted by Tehniyat View Post
    One thread will push back the data for every 1 sec and other thread will pop out the data from deque for every 0.5 secs.
    In a producer-consumer situation like this it's common to use a concurrent queue. Items are pushed into the queue (enqueued) at the back and they are popped (dequeued) at the front. This makes a queue a first-in-first-out (FIFO) data structure. The items leave the queue in the same order as they entered it. Somewhat confusingly, there's also a data structure called a deque. It's a double-ended queue meaning it allows for pushing and popping at both ends. From your problem description I take it that it is a queue you require.

    C++ itself doesn't offer a concurrent queue at the moment but there are external libraries available that do, for example from Microsoft,


    and from Intel,


    I've tried both and they work fine of course but when C++ 11 arrived I went for a pure C++ implementation I found in a book. Here's my version of it,

    #ifndef LOCK_QUEUE_ONCE
    #define LOCK_QUEUE_ONCE
    // Threadsafe queue
    // (see C++ Concurrency in action by Anthony Williams page 154)
    #include <queue>
    #include <memory>
    #include <condition_variable>
    #include <mutex>
    namespace maux {
    	template<typename T>
    	class Lockqueue {
    		using Mutex = std::mutex;
    		void push(T value) { // push
    			std::lock_guard<Mutex> lock(mutex);
    		bool try_pop(T& value) { // non-blocking pop
    			std::lock_guard<Mutex> lock(mutex);
    			if (queue.empty()) return false;
    			value = std::move(queue.front());
    			return true;
    		T wait_pop() { // blocking pop
    			std::unique_lock<Mutex> lock(mutex);
    			condition.wait(lock, [this]{return !queue.empty(); });
    			T const value = std::move(queue.front());
    			return value;
    		int size() const { // queue size
    			std::lock_guard<Mutex> lock(mutex);
    			return static_cast<int>(queue.size());
    		mutable Mutex mutex;
    		std::queue<T> queue;
    		std::condition_variable condition;
    It simply adds synchronization to the standard STL data structure std::queue.

    To use maux::Lockqueue I suggest you allocate one object on the heap using new. The returned pointer is then passed to the two threads when they are started. The threads can use this pointer to access the same maux::Lockqueue object and thereby pass items from one to the other in a threadsafe manner.
    Last edited by wolle; December 25th, 2018 at 01:39 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)