先把代码贴上来,有时间再整理吧。。因为工作中用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 == ){
bMutex.lock();
}
readerCount++;
rdcnMutex.unlock(); printMutex.lock();
sleep();
read();
printMutex.unlock(); rdcnMutex.lock();
readerCount--;
if(readerCount == ){
bMutex.unlock();
}
rdcnMutex.unlock();
}
}; int Reader::readerCount = ;
QMutex Reader::rdcnMutex;
QMutex Reader::printMutex; class Writer:public QThread{
public:
void write(){
qsrand(QTime(,,).secsTo(QTime::currentTime()));
buffer = qrand() % ;
cout<<"写者写入:"<<buffer<<endl;
}
protected:
void run(){
bMutex.lock();
sleep();
write();
bMutex.unlock();
}
}; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); buffer = -;
const int count = ;
Writer writer[count];
Reader reader[count];
for(int i = ; i < count; i++ ){
writer[i].start();
reader[i].start();
}
for(int i = ; 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 == ){
bMutex.lock();
}
readerCount++;
rdcnMutex.unlock();
queueMutex.unlock(); sleep();
read(); rdcnMutex.lock();
readerCount--;
if(readerCount == ){
bMutex.unlock();
}
rdcnMutex.unlock();
}
};
int Reader::readerCount = ;
QMutex Reader::rdcnMutex;
QMutex Reader::printMutex; class Writer: public QThread{
private:
static int writerCount;
static QMutex wtcnMutex;
public:
void write(){
qsrand(QTime(,,).secsTo(QTime::currentTime()));
buffer = qrand() % ;
cout<<"写者写入:"<<buffer<<endl;
}
protected:
void run(){
wtcnMutex.lock();
if(writerCount == ){
queueMutex.lock();
}
writerCount++;
wtcnMutex.unlock(); bMutex.lock();
sleep();
write();
bMutex.unlock(); wtcnMutex.lock();
writerCount--;
if(writerCount == ){
queueMutex.unlock();
}
wtcnMutex.unlock();
} };
int Writer::writerCount = ;
QMutex Writer::wtcnMutex; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); buffer = -;
const int count = ;
Writer writer[count];
Reader reader[count];
for(int i = ; i < count; i++ ){
writer[i].start();
reader[i].start();
}
for(int i = ; 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 == ){
bMutex.lock();
}
readerCount++;
rdcnMutex.unlock();
queueMutex.unlock(); sleep();
read(); rdcnMutex.lock();
readerCount--;
if(readerCount == ){
bMutex.unlock();
}
rdcnMutex.unlock();
}
};
int Reader::readerCount = ;
QMutex Reader::rdcnMutex;
QMutex Reader::printMutex; class Writer: public QThread{
private:
public:
void write(){
qsrand(QTime(,,).secsTo(QTime::currentTime()));
buffer = qrand() % ;
cout<<"写者写入:"<<buffer<<endl;
}
protected:
void run(){
queueMutex.lock();
bMutex.lock();
queueMutex.unlock();
sleep();
write();
bMutex.unlock(); } }; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); buffer = -;
const int count = ;
Writer writer[count];
Reader reader[count];
for(int i = ; i < count; i++ ){
writer[i].start();
reader[i].start();
}
for(int i = ; i < count; i++ ){
writer[i].wait();
reader[i].wait();
}
return a.exec();
}
04-21 08:18