C++ (Qt)//Custom class - thread safe queue.class SafeQueue<T> : QQueue { public: //Constuctor. SafeQueue() : Queue<T> {} //Enqueue void enqueue(T) { QMutexLocker locker(&mutex); QQueue::enqueue(T); } //Dequeue. T dequeue(T) { QMutexLocker locker(&mutex); return QQueue::dequeue(); } private: QMutex mutex;}
C++ (Qt)//Custom class - thread safe queue.class SafeQueue<T> : QQueue { public: //Constuctor. SafeQueue() : Queue<T>, mAbortFlag(false) {} void setAbort( void ) { mAbortFlag = true; } //Enqueue void enqueue( const T & data ) { if (mAbortFlag) return; while (!mLock.testAndSetOrdered(0, 1)) { if (mAbortFlag) return; QThread::yieldCurrentThread() } QQueue::enqueue(data); mLock = 0; } //Dequeue. T dequeue( void ) { while (true) { if (mAbortFlag) return T(); if (!mLock.testAndSetOrdered(0, 1)) QThread::yieldCurrentThread(); else { if (!size()) { mLock = 0; QThread::yieldCurrentThread(); continue; } T data = QQueue::dequeue(); mLock = 0; return data; } } return T(); } private: bool mAbortFlag; QAtomicInt mLock;}