First put the code up, there is time to organize it. Because QT is used in work, it is realized by qt .... Just work, and busy!
Three methods, reader first, writer priority and fair competition.
Readers first
#include <QCoreApplication>#include<QThread>#include<iostream>#include<QMutex>#include<QTime>#include<QtGlobal>using namespacestd;intBuffer//Critical ResourcesQmutex Bmutex;classReader: Publicqthread{Private: Static intReadercount; StaticQmutex Rdcnmutex; StaticQmutex Printmutex; Public: voidRead () {cout<<"Reader read out:"<<buffer<<Endl; }protected: voidrun () {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 (); }};intReader::readercount =0; Qmutex Reader::rdcnmutex; Qmutex Reader::p Rintmutex;classWriter: Publicqthread{ Public: voidwrite () {Qsrand (Qtime (0,0,0). Secsto (Qtime::currenttime ())); Buffer= Qrand ()% -; cout<<"Written by:"<<buffer<<Endl; }protected: voidrun () {Bmutex.Lock(); Sleep (1); Write (); Bmutex.unlock (); }};intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); Buffer= -1; Const intCount =Ten; Writer Writer[count]; Reader Reader[count]; for(inti =0; I < count; i++) {Writer[i].start (); Reader[i].start (); } for(inti =0; I < count; i++) {writer[i].wait (); Reader[i].wait (); } returna.exec ();}
Written by first:
#include <QCoreApplication>#include<QMutex>#include<QtGlobal>#include<QThread>#include<iostream>#include<QtGlobal>#include<QTime>using namespacestd;intBuffer//Critical ResourcesQmutex Bmutex; Qmutex Queuemutex;classReader: Publicqthread{Private: Static intReadercount; StaticQmutex Rdcnmutex; StaticQmutex Printmutex; Public: voidRead () {Printmutex.Lock(); cout<<"Reader read out:"<<buffer<<Endl; Printmutex.unlock (); }protected: voidrun () {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 (); }};intReader::readercount =0; Qmutex Reader::rdcnmutex; Qmutex Reader::p Rintmutex;classWriter: Publicqthread{Private: Static intWritercount; StaticQmutex Wtcnmutex; Public: voidwrite () {Qsrand (Qtime (0,0,0). Secsto (Qtime::currenttime ())); Buffer= Qrand ()% -; cout<<"Written by:"<<buffer<<Endl; }protected: voidrun () {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 (); }};intWriter::writercount =0; Qmutex Writer::wtcnmutex;intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); Buffer= -1; Const intCount =Ten; Writer Writer[count]; Reader Reader[count]; for(inti =0; I < count; i++) {Writer[i].start (); Reader[i].start (); } for(inti =0; I < count; i++) {writer[i].wait (); Reader[i].wait (); } returna.exec ();}
Fair competition
#include <QCoreApplication>#include<QMutex>#include<QtGlobal>#include<QThread>#include<iostream>#include<QtGlobal>#include<QTime>using namespacestd;intBuffer//Critical ResourcesQmutex Bmutex; Qmutex Queuemutex;classReader: Publicqthread{Private: Static intReadercount; StaticQmutex Rdcnmutex; StaticQmutex Printmutex; Public: voidRead () {Printmutex.Lock(); cout<<"Reader read out:"<<buffer<<Endl; Printmutex.unlock (); }protected: voidrun () {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 (); }};intReader::readercount =0; Qmutex Reader::rdcnmutex; Qmutex Reader::p Rintmutex;classWriter: Publicqthread{Private: Public: voidwrite () {Qsrand (Qtime (0,0,0). Secsto (Qtime::currenttime ())); Buffer= Qrand ()% -; cout<<"Written by:"<<buffer<<Endl; }protected: voidrun () {Queuemutex.Lock(); Bmutex.Lock(); Queuemutex.unlock (); Sleep (1); Write (); Bmutex.unlock (); }};intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); Buffer= -1; Const intCount =Ten; Writer Writer[count]; Reader Reader[count]; for(inti =0; I < count; i++) {Writer[i].start (); Reader[i].start (); } for(inti =0; I < count; i++) {writer[i].wait (); Reader[i].wait (); } returna.exec ();}
Multi-Threaded reader Writer's problem--using QT to realize