先把代码贴上来,有时间再整理吧。。因为工作中用Qt,所以用Qt实现的。。。。刚上班,,忙!
三种方法,读者优先,写者优先和公平竞争。。
读者优先
#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(); }