澶氱嚎绋嬭鑰呭啓鑰呴棶棰?-鐢≦T瀹炵幇
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了澶氱嚎绋嬭鑰呭啓鑰呴棶棰?-鐢≦T瀹炵幇相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/start' title='start'>start
std core EAP cout art har names ++鍏堟妸浠g爜璐翠笂鏉ワ紝鏈夋椂闂村啀鏁寸悊鍚с€傘€傚洜涓哄伐浣滀腑鐢≦t锛屾墍浠ョ敤Qt瀹炵幇鐨勩€傘€傘€傘€傚垰涓婄彮锛岋紝蹇欙紒
涓夌鏂规硶锛岃鑰呬紭鍏堬紝鍐欒€呬紭鍏堝拰鍏钩绔炰簤銆傘€?/p>
璇昏€呬紭鍏?/p>
#include <QCoreApplication> #include <QThread> #include <iostream> #include <QMutex> #include <QTime> #include <QtGlobal> using namespace std; int buffer;//涓寸晫璧勬簮 QMutex bMutex; class Reader:public QThread{ private: static int readerCount; static QMutex rdcnMutex; static QMutex printMutex; public: void read(){ cout<<"璇昏€呰鍑猴細"<<buffer<<endl; } protected: void run(){ rdcnMutex.lock(); if(readerCount == 0){ bMutex.lock(); } readerCount++; rdcnMutex.unlock(); printMutex.lock(); sleep(1); read(); printMutex.unlock(); rdcnMutex.lock(); readerCount--; if(readerCount == 0){ bMutex.unlock(); } rdcnMutex.unlock(); } }; int Reader::readerCount = 0; QMutex Reader::rdcnMutex; QMutex Reader::printMutex; class Writer:public QThread{ public: void write(){ qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); buffer = qrand() % 100; cout<<"鍐欒€呭啓鍏ワ細"<<buffer<<endl; } protected: void run(){ bMutex.lock(); sleep(1); write(); bMutex.unlock(); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); buffer = -1; const int count = 10; Writer writer[count]; Reader reader[count]; for(int i = 0; i < count; i++ ){ writer[i].start(); reader[i].start(); } for(int i = 0; i < count; i++ ){ writer[i].wait(); reader[i].wait(); } return a.exec(); }
鍐欒€呬紭鍏堬細
#include <QCoreApplication> #include <QMutex> #include <QtGlobal> #include <QThread> #include <iostream> #include <QtGlobal> #include <QTime> using namespace std; int buffer;//涓寸晫璧勬簮 QMutex bMutex; QMutex queueMutex; class Reader: public QThread{ private: static int readerCount; static QMutex rdcnMutex; static QMutex printMutex; public: void read(){ printMutex.lock(); cout<<"璇昏€呰鍑猴細"<<buffer<<endl; printMutex.unlock(); } protected: void run(){ queueMutex.lock(); rdcnMutex.lock(); if(readerCount == 0){ bMutex.lock(); } readerCount++; rdcnMutex.unlock(); queueMutex.unlock(); sleep(1); read(); rdcnMutex.lock(); readerCount--; if(readerCount == 0){ bMutex.unlock(); } rdcnMutex.unlock(); } }; int Reader::readerCount = 0; QMutex Reader::rdcnMutex; QMutex Reader::printMutex; class Writer: public QThread{ private: static int writerCount; static QMutex wtcnMutex; public: void write(){ qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); buffer = qrand() % 100; cout<<"鍐欒€呭啓鍏ワ細"<<buffer<<endl; } protected: void run(){ wtcnMutex.lock(); if(writerCount == 0){ queueMutex.lock(); } writerCount++; wtcnMutex.unlock(); bMutex.lock(); sleep(1); write(); bMutex.unlock(); wtcnMutex.lock(); writerCount--; if(writerCount == 0){ queueMutex.unlock(); } wtcnMutex.unlock(); } }; int Writer::writerCount = 0; QMutex Writer::wtcnMutex; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); buffer = -1; const int count = 10; Writer writer[count]; Reader reader[count]; for(int i = 0; i < count; i++ ){ writer[i].start(); reader[i].start(); } for(int i = 0; i < count; i++ ){ writer[i].wait(); reader[i].wait(); } return a.exec(); }
鍏钩绔炰簤
#include <QCoreApplication> #include <QMutex> #include <QtGlobal> #include <QThread> #include <iostream> #include <QtGlobal> #include <QTime> using namespace std; int buffer;//涓寸晫璧勬簮 QMutex bMutex; QMutex queueMutex; class Reader: public QThread{ private: static int readerCount; static QMutex rdcnMutex; static QMutex printMutex; public: void read(){ printMutex.lock(); cout<<"璇昏€呰鍑猴細"<<buffer<<endl; printMutex.unlock(); } protected: void run(){ queueMutex.lock(); rdcnMutex.lock(); if(readerCount == 0){ bMutex.lock(); } readerCount++; rdcnMutex.unlock(); queueMutex.unlock(); sleep(1); read(); rdcnMutex.lock(); readerCount--; if(readerCount == 0){ bMutex.unlock(); } rdcnMutex.unlock(); } }; int Reader::readerCount = 0; QMutex Reader::rdcnMutex; QMutex Reader::printMutex; class Writer: public QThread{ private: public: void write(){ qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); buffer = qrand() % 100; cout<<"鍐欒€呭啓鍏ワ細"<<buffer<<endl; } protected: void run(){ queueMutex.lock(); bMutex.lock(); queueMutex.unlock(); sleep(1); write(); bMutex.unlock(); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); buffer = -1; const int count = 10; Writer writer[count]; Reader reader[count]; for(int i = 0; i < count; i++ ){ writer[i].start(); reader[i].start(); } for(int i = 0; i < count; i++ ){ writer[i].wait(); reader[i].wait(); } return a.exec(); }
以上是关于澶氱嚎绋嬭鑰呭啓鑰呴棶棰?-鐢≦T瀹炵幇的主要内容,如果未能解决你的问题,请参考以下文章