2022년 3월 21일 월요일

Qt QQueue 로 만든 간단한 template 기반 thread safe Queue 클래스

 #ifndef MYBUFFERQUEUE_H

#define MYBUFFERQUEUE_H

#include <QQueue>
#include <QMutexLocker>

template<typename T>
class MyBufferQueue
{
public:
    MyBufferQueue(int maxCount);
    virtual ~MyBufferQueue();

    int push(T *buffer);
    T* pop();

    void clear();
    int count();

private:
    QQueue<T*>      m_queue;
    QMutex          m_mutex;
    int             m_nMaxCount;
};

template <typename T>
MyBufferQueue<T>::MyBufferQueue(int maxCount)
{
    m_nMaxCount = maxCount;
}

template <typename T>
MyBufferQueue<T>::~MyBufferQueue()
{
    clear();
}

template <typename T>
int MyBufferQueue<T>::push(T *buffer)
{
    QMutexLocker locker(&m_mutex);

    if (m_nMaxCount > 0) {
        if (m_queue.count() >= m_nMaxCount)
            return -1;
    }

    m_queue.enqueue(buffer);

    return m_queue.count();
}

template <typename T>
T* MyBufferQueue<T>::pop()
{
    QMutexLocker locker(&m_mutex);

    T *image = NULL;
    if (!m_queue.isEmpty()) image = m_queue.dequeue();

    return image;
}

template <typename T>
void MyBufferQueue<T>::clear()
{
    QMutexLocker locker(&m_mutex);
    qDeleteAll(m_queue);
    m_queue.clear();
}

template <typename T>
int MyBufferQueue<T>::count()
{
    QMutexLocker locker(&m_mutex);
    return m_queue.count();
}

class AudioBuffer {
public:
    AudioBuffer() {
        data = NULL;
        size = 0;
    }

    AudioBuffer(char *_data, int _size) {
        data = new char[_size];
        memcpy(data, _data, _size);
        size = _size;
    }

    virtual ~AudioBuffer() {
        if (size > 0) delete[] data;
    }

public:
    char*   data;
    int     size;
};

#endif // MyBufferQueue_H




< 사용법 >

MyBufferQueue<AudioBuffer>*   m_pSendQue = new MyBufferQueue<AudioBuffer>(100);
...
AudioBuffer *pSendBuffer = new AudioBuffer(data, size);
m_pSendQue->push(pSendBuffer);
...
...
AudioBuffer* pBuffer = m_SendQue->pop();
...
delete pBuffer;