• 多线程读者写者问题--用QT实现


    先把代码贴上来,有时间再整理吧。。因为工作中用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();
    }
  • 相关阅读:
    02-Java 数组和排序算法
    Spring Security 入门
    mysql外键理解
    redis能否对set数据的每个member设置过期时间
    Redis sortedset实现元素自动过期
    mysql之触发器trigger
    一篇很棒的 MySQL 触发器学习教程
    mysql触发器
    云游戏
    mysql触发器个人实战
  • 原文地址:https://www.cnblogs.com/qingergege/p/9296803.html
Copyright © 2020-2023  润新知