参考:

Qt线程的简单使用--QReadWriteLock的用法_qt的读写锁怎么应用-CSDN博客

应用场景:多个线程同时进行读操作。

比如:100个线程进行读操作,1个线程进行写操作。

示例1:

#include <QObject>
#include <QThread>
class Read_thread : public QThread
{
    Q_OBJECT
public:
    explicit Read_thread(QObject *parent = nullptr);
protected:
    void run();
};

class Write_thread : public QThread
{
    Q_OBJECT
public:
    explicit Write_thread(QObject *parent = nullptr);
protected:
    void run();
};
#include <QReadWriteLock>
int counter=0;
QReadWriteLock lock;

void Read_thread::run()
{
    lock.lockForRead();
    for(int i=0;i<5;i++)
    {
        this->msleep(10);
        qDebug()<<"Read_thread:"<<QThread::currentThreadId()<<"   "<<counter;
    }
    lock.unlock();
}
void Write_thread::run()
{
    lock.lockForWrite();
    for(int i=0;i<5;i++)
    {
        counter++;
        this->msleep(10);
        qDebug()<<"Write_thread:"<<QThread::currentThreadId()<<"   "<<counter;
    }
    lock.unlock();
}
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    qDebug()<<"main:"<<QThread::currentThreadId();
    connect(this,SIGNAL(destroyed()),this,SLOT(quitThreadSlot()));
    t=new Read_thread(this);
    t->start();

    t1=new Read_thread(this);
    t1->start();

    t2=new Write_thread(this);
    t2->start();

    t3=new Write_thread(this);
    t3->start();
}

void Widget::quitThreadSlot()
{
    t1->quit();
    t1->wait();

    t->quit();
    t->wait();

    t2->quit();
    t2->wait();

    t3->quit();
    t3->wait();
}

 示例2:修改部分代码:

    t=new Read_thread(this);
    t->start();

    QThread::msleep(1000);
    t2=new Write_thread(this);
    t2->start();

    t3=new Write_thread(this);
    t3->start();

    t1=new Read_thread(this);
    t1->start();

示例3:QReadLocker和QWriteLocker的使用

void Read_thread::run()
{
    QReadLocker locker(&lock);
    for(int i=0;i<5;i++)
    {
        this->msleep(10);
        qDebug()<<"Read_thread:"<<QThread::currentThreadId()<<"   "<<counter;
    }
}
void Write_thread::run()
{
    QWriteLocker locker(&lock);
    for(int i=0;i<5;i++)
    {
        counter++;
        this->msleep(10);
        qDebug()<<"Write_thread:"<<QThread::currentThreadId()<<"   "<<counter;
    }
}

 这样写只是更简单了一些,不需要再locked(),unlocked()了。

 

 

01-19 21:16