#include <iostream>#include <QThread>#include <QList>#include <QMutex>class MyQueue{ QMutex lock; QList <int> list;public: int i; int getLast() {lock.lock(); i = list.last(); lock.unlock(); return i;} void addLast(int i) {lock.lock(); list.push_back(i); lock.unlock();}};class WriterThread : public QThread {public: WriterThread(MyQueue *queue, QObject *parent = 0);private: MyQueue *myQueue;protected: void run();};WriterThread::WriterThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){}void WriterThread::run(){ for(int i = 0; i <= 100; i++) { myQueue->addLast(i); this->msleep(100); }}class ReaderThread : public QThread {public: ReaderThread(MyQueue *queue, QObject *parent = 0);private: MyQueue *myQueue;protected: void run();};ReaderThread::ReaderThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){}void ReaderThread::run(){ int i = 0; while(i < 100) { i = myQueue->getLast(); std::cout << i << std::endl; }}int main(){ MyQueue mq; WriterThread a(&mq); ReaderThread b(&mq); a.start(); b.start(); a.wait(); b.wait();}
C++ (Qt)#include <iostream>#include <QThread>#include <QList>#include <QMutex> class MyQueue{ QMutex mutex; QList <int> list;public: bool getLast( int & i ) { QMutexLocker lock(&mutex); if (!list.size()) return false; i = list.last(); return true; } void addLast( int i ) { QMutexLocker lock(&mutex); list.push_back(i); }}; class WriterThread : public QThread { public: WriterThread(MyQueue *queue, QObject *parent = 0);signals: void NewData( void ); void EndJob ( void ); private: MyQueue *myQueue;protected: void run();}; WriterThread::WriterThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){} void WriterThread::run(){ for(int i = 0; i <= 100; i++) { myQueue->addLast(i); emit NewData(); this->msleep(100); } emit EndJob();} class ReaderThread : public QThread { public: ReaderThread(MyQueue *queue, QObject *parent = 0);public slots: void NewData( void );private: MyQueue *myQueue;}; ReaderThread::ReaderThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){} void ReaderThread::NewData( void ){ int i; if (myQueue->getLast(i)) std::cout << i << std::endl;} int main(){ MyQueue mq; WriterThread a(&mq); ReaderThread b(&mq); QObject::connect(&a, SIGNAL(NewData()), &b, SLOT(NewData()), Qt::QueuedConnection); QObject::connect(&a, SIGNAL(EndJob()), &b, SLOT(quit()), Qt::QueuedConnection); a.start(); b.start(); a.wait(); b.wait();}
C++ (Qt)#include <QThread>#include <QList> class WriterThread : public QThread{Q_OBJECT public: WriterThread(QObject *parent = 0) : QThread(parent) {}; signals: void NewData(int); protected: void run();}; void WriterThread::run(){ for(int i = 0; i <= 100; ++i) emit NewData(i);} class ReaderThread : public QThread{public: ReaderThread(QObject *parent = 0) : QThread(parent) {}; public slots: void NewData(int i); private: QList <int> list;}; void ReaderThread::NewData(int i){ //Тут обрабатывать очередь list.push_back(i); if (list.size > listMaxSize) list.removeFirst(); //и т. д. и т. п.} int main(){ WriterThread a(); ReaderThread b(); connect(&a, SIGNAL(NewData(int)), &b, SLOT(NewData(int)), Qt::QueuedConnection); a.start(); b.start(); a.wait(); b.wait();}
#ifndef MYQUEUE_H#define MYQUEUE_H#include <QList>#include <QMutex>class MyQueue{ QMutex mutex; QList <int> list;public: bool getLast( int & i ) { QMutexLocker lock(&mutex); if (!list.size()) return false; i = list.last(); return true; } void addLast( int i ) { QMutexLocker lock(&mutex); list.push_back(i); }};#endif // MYQUEUE_H
#ifndef READERTHREAD_H#define READERTHREAD_H#include <QThread>#include "MyQueue.h"class ReaderThread : public QThread {Q_OBJECTpublic: ReaderThread(MyQueue *queue, QObject *parent = 0);public slots: void NewData( void );private: MyQueue *myQueue;};#endif // READERTHREAD_H
#ifndef WRITERTHREAD_H#define WRITERTHREAD_H#include <QThread>#include "MyQueue.h"class WriterThread : public QThread {Q_OBJECTpublic: WriterThread(MyQueue *queue, QObject *parent = 0);signals: void NewData( void ); void EndJob ( void );private: MyQueue *myQueue;protected: void run();};#endif // WRITERTHREAD_H
#include <iostream>#include "QApplication.h"#include "WriterThread.h"#include "ReaderThread.h"WriterThread::WriterThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){}void WriterThread::run(){ for(int i = 0; i <= 100; i++) { myQueue->addLast(i); emit NewData(); this->msleep(100); } emit EndJob();}ReaderThread::ReaderThread(MyQueue *queue, QObject *parent) : QThread(parent), myQueue(queue){}void ReaderThread::NewData( void ){ int i; if (myQueue->getLast(i)) std::cout << i << std::endl;}int main(int argc, char *argv[]){ QApplication app(argc, argv); MyQueue mq; WriterThread a(&mq); ReaderThread b(&mq); QObject::connect(&a, SIGNAL(NewData()), &b, SLOT(NewData()), Qt::QueuedConnection); QObject::connect(&a, SIGNAL(EndJob()), &b, SLOT(quit()), Qt::QueuedConnection); a.start(); b.start(); a.wait(); b.wait(); return app.exec();}