[Interest] ThreadSafeQueue class deadlocks after consumer acquire

Nilesh Kokane nilesh.kokane05 at gmail.com
Sat Jul 30 21:17:27 CEST 2016


Hello,

I've a threadsafe queue designed as below. I'm pushing data from one
thread and popping from the other thread, but the pop is not working.
The movement I  consumer.acquire() in the pop function from the other
thread it goes to dead lock.


#define MAX_COUNT 1000

static unsigned int count = MAX_COUNT;


static QSemaphore producer(count);

static QSemaphore consumer;



template<class T>

class ThreadSafeQueue

{

public:
    ThreadSafeQueue()
    {
    }
    ~ThreadSafeQueue()
    {

    }

    void clean()
    {
        m_queue.clear();
    }

    void push(const T& t)
    {
        qDebug() << "value in push" << t;
        producer.acquire();
        QMutexLocker lock(&m_mutex);
        m_queue.enqueue(t);
        consumer.release(1);
    }

    T pop()
    {
        consumer.acquire();
        QMutexLocker lock(&m_mutex);
        T i = m_queue.dequeue();
        producer.release();
        return i;
    }

private:
    QQueue<T> m_queue;
    QMutex m_mutex;
};


The push function is called for 1000 times though. Any clue?

-- 
          Nilesh Kokane



More information about the Interest mailing list