Multi-Threaded reader Writer's problem--using QT to realize

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.