class MessageStack{ QReadWriteLock lock; QStack<QString> stack;public: QString i; int get() { lock.lockForRead(); i = stack.pop(); lock.unlock(); return i; } void add(int i) { lock.lockForWrite(); list.push(i); lock.unlock();}};
C++ (Qt)class MessageStack{ QReadWriteLock lock; QStack<QString> stack; QSemaphore mSemaphore;public: QString i; int get() { mSemaphore.acquire(); // ждем когда можно забрать данные lock.lockForRead(); i = stack.pop(); lock.unlock(); return i; } void add(int i) { lock.lockForWrite(); list.push(i); lock.unlock(); mSemaphore.release(); // теперь get может забирать }};
#pragma once#include <QStack>#include <QMutex>#include <QString>class GHQtCommandStack{public: GHQtCommandStack(void); ~GHQtCommandStack(void); QString get(); void add(QString str);private: QMutex mutex; QStack<QString> stack;};
#include "GHQtCommandStack.h"GHQtCommandStack::GHQtCommandStack(void){}GHQtCommandStack::~GHQtCommandStack(void){}QString GHQtCommandStack::get(){ QString result; mutex.lock(); if(stack.isEmpty()){ mutex.unlock(); return "empty"; } else { result=stack.pop(); mutex.unlock(); return result; }}void GHQtCommandStack::add(QString str){ mutex.lock(); stack.push(str); mutex.unlock();}