qt 添加线程锁 有多少种方式,并分别举例

1.概要

qt 添加线程锁 有多少种方式,并分别举例

2.内容

在 Qt 中,添加线程锁有多种方式,以下是每种方式的具体举例:

1. QMutex

功能QMutex 是 Qt 中最常用的互斥锁(mutex),用于保护共享数据资源,确保同一时刻只有一个线程能够访问该资源。

举例

#include <QMutex>
#include <QDebug>

QMutex mutex;
int sharedCounter = 0;

void incrementCounter() {
    mutex.lock();
    ++sharedCounter;
    qDebug() << "Counter incremented to" << sharedCounter;
    mutex.unlock();
}

2. QMutexLocker

功能QMutexLocker 是一个智能锁封装类,它基于 RAII(资源获取即初始化)原则,可以在作用域结束时自动释放锁,从而避免忘记解锁或异常导致未解锁的情况。

举例

#include <QMutex>
#include <QMutexLocker>
#include <QDebug>

QMutex mutex;
int sharedCounter = 0;

void incrementCounter() {
    QMutexLocker locker(&mutex);
    ++sharedCounter;
    qDebug() << "Counter incremented to" << sharedCounter;
    // 当 locker 对象离开作用域时,会自动调用其析构函数,从而释放锁
}

3. QReadWriteLock

功能QReadWriteLock 是一个读写锁,允许多个线程同时读取共享资源,但在写线程访问资源时,其他线程不能同时读取或写入。

举例

#include <QReadWriteLock>
#include <QDebug>

QReadWriteLock rwLock;
int sharedData = 0;

void readData() {
    rwLock.lockForRead();
    qDebug() << "Read value:" << sharedData;
    rwLock.unlock();
}

void writeData(int newValue) {
    rwLock.lockForWrite();
    sharedData = newValue;
    qDebug() << "Write value:" << sharedData;
    rwLock.unlock();
}

4. QReadLocker 和 QWriteLocker

功能QReadLocker 和 QWriteLocker 分别是 QReadWriteLock 的 RAII 类,用于简化对读锁和写锁的管理。

举例

#include <QReadWriteLock>
#include <QReadLocker>
#include <QWriteLocker>
#include <QDebug>

QReadWriteLock rwLock;
int sharedData = 0;

void readData() {
    QReadLocker locker(&rwLock);
    qDebug() << "Read value:" << sharedData;
    // 当 locker 对象离开作用域时,会自动释放读锁
}

void writeData(int newValue) {
    QWriteLocker locker(&rwLock);
    sharedData = newValue;
    qDebug() << "Write value:" << sharedData;
    // 当 locker 对象离开作用域时,会自动释放写锁
}

5. QSemaphore

功能QSemaphore 是一个信号量机制,用于限制同时访问共享资源的线程数。

举例

#include <QSemaphore>
#include <QDebug>
#include <QThread>
#include <QCoreApplication>

QSemaphore semaphore(1); // 初始信号量计数为1

void accessResource() {
    semaphore.acquire(); // 请求信号量
    qDebug() << "Thread" << QThread::currentThreadId() << "is accessing the resource.";
    QThread::sleep(1); // 模拟资源访问时间
    semaphore.release(); // 释放信号量
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QThread thread1, thread2;

    QObject::connect(&thread1, &QThread::started, accessResource);
    QObject::connect(&thread2, &QThread::started, accessResource);

    thread1.start();
    thread2.start();

    thread1.wait();
    thread2.wait();

    return a.exec();
}

6. QWaitCondition

功能QWaitCondition 是一个条件变量,允许线程在某些条件满足时被唤醒。

举例

#include <QMutex>
#include <QWaitCondition>
#include <QDebug>
#include <QThread>
#include <QCoreApplication>

QMutex mutex;
QWaitCondition condition;
bool ready = false;

void workerThread() {
    mutex.lock();
    while (!ready) {
        condition.wait(&mutex); // 等待条件变量变为 true
    }
    qDebug() << "Worker thread is working.";
    mutex.unlock();
}

void setReady() {
    mutex.lock();
    ready = true;
    condition.wakeOne(); // 唤醒一个等待的线程
    mutex.unlock();
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QThread thread;

    QObject::connect(&thread, &QThread::started, workerThread);
    thread.start();

    QThread::sleep(2); // 模拟一些工作

    setReady();

    thread.wait();

    return a.exec();
}

7. QAtomicInteger / QAtomicPointer

功能QAtomicInteger 和 QAtomicPointer 用于实现原子操作,确保对共享数据或指针的访问是线程安全的。

举例(QAtomicInteger):

#include <QAtomicInteger>
#include <QThread>
#include <QDebug>

QAtomicInteger<int> counter(0);

void incrementCounter() {
    for (int i = 0; i < 100; ++i) {
        counter.fetchAndAddRelaxed(1);
    }
}

int main(int argc, char *argv[]) {
    QThread thread1(incrementCounter);
    QThread thread2(incrementCounter);

    thread1.start();
    thread2.start();

    thread1.wait();
    thread2.wait();

    qDebug() << "Final counter value:" << counter.loadAcquire();
    return 0;
}

这些例子展示了在 Qt 中添加线程锁的不同方式,每种方式都有其适用的场景和优缺点。在选择使用哪种锁时,应根据具体的应用场景和性能需求来决定。

3.关联链接

qt 添加线程锁-优快云博客

qt 添加线程锁 有多少种方式-优快云博客 

qt 添加线程锁 有多少种方式,并分别举例-优快云博客 

qt 设置线程锁的方式有多少种,并分别举例-优快云博客 

qt 设置线程锁的方式有多少种-优快云博客 

qt 设置线程锁-优快云博客 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值