赞
踩
class MyThread : public QThread { Q_OBJECT signals: void updateUI(int value); public: void run() override { for (int i = 0; i < 10; ++i) { emit updateUI(i); // 发出信号更新UI QThread::sleep(1); } } }; // 在主窗口类中连接信号和槽 connect(ptMyThread, &MyThread::updateUI, this, &MainWindow::updateUIFunction);
QMutex buflock;//其实就是互斥信号量。
buflock.lock();
buflock.unlock();
//rethread.h #ifndef RETHREAD_H #define RETHREAD_H #include<QThread> #include<QMutex> struct msg_struct{ int temp; int shidu; char des[128]; }; class thread_write:public QThread{ public: thread_write(); thread_write(struct msg_struct *pmsg,QMutex *pMutex);//写 ~thread_write(); void run() override;//写。重写 private: struct msg_struct *pShareMsg; QMutex *pMutex; }; class thread_read:public QThread{ public: thread_read(struct msg_struct *pmsg,QMutex *pMutex);//写 thread_read(); ~thread_read(); void run() override;//重写 private: struct msg_struct *pShareMsg; QMutex *pMutex; }; #endif // RETHREAD_H //rethread.cpp #include "rethread.h" #include<QDebug> thread_write::thread_write() { } thread_write::thread_write(msg_struct *pmsg, QMutex *pMutex) { pShareMsg = pmsg; this->pMutex=pMutex; } thread_write::~thread_write() { } void thread_write::run() { char ch = 'A'; while(1){ pMutex->lock(); for(int i= 0;i<127;i++){ pShareMsg->des[i]=ch; if(i%20==0){ QThread::sleep(1); } } pMutex->unlock(); QThread::msleep(10); ch++; } } thread_read::thread_read(msg_struct *pmsg, QMutex *pMutex) { pShareMsg = pmsg; this->pMutex = pMutex; } thread_read::thread_read() { } thread_read::~thread_read() { } void thread_read::run() { QThread::sleep(1); while(1){ pMutex->lock(); qDebug()<<"thread read"<<__func__<<" "<<pShareMsg->des; pMutex->unlock(); QThread::sleep(5); } } //widget.h #ifndef WIDGET_H #define WIDGET_H #include"rethread.h" #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private: Ui::Widget *ui; struct msg_struct *pShareMsg; QMutex *pMutex; thread_read *pThreadRead; thread_write *pThreadWrite; }; #endif // WIDGET_H //widget.cpp #include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); pShareMsg = new struct msg_struct; pMutex = new QMutex; pThreadRead = new thread_read(pShareMsg,pMutex); pThreadWrite = new thread_write(pShareMsg,pMutex); pThreadWrite->start(); pThreadRead->start(); } Widget::~Widget() { delete ui; }
//widget.h
QMutex *pMutex;
//widget.cpp
pMutex = new QMutex;
pMutex.lock();
pMutex.unlock();
pMutex.try_Lock();//括号内可以有参数,以毫秒为单位,表示最多等待多少毫秒
//widget.h
QReadWriteLock *pRWLock;
//Widget.cpp
pRWLock = new QReadWriteLock;
QMutex mutex;
QReadWriteLock readWriteLock;
QWaitCondition condition;
方法名 | 描述 | 参数 | 返回值 |
---|---|---|---|
构造函数 | |||
QSemaphore(int initialCount = 1, int maxCount = 1) | 创建一个信号量,初始计数和最大计数。 | initialCount :初始计数值maxCount :最大计数值 | 无 |
基本方法 | |||
acquire(int num = 1) | 获取 num 个资源,若资源不足,线程阻塞。 | num :需要获取的资源数量 | 无 |
release(int num = 1) | 释放 num 个资源,增加信号量的计数器。 | num :需要释放的资源数量 | 无 |
带超时的方法 | |||
acquire(int num, unsigned long timeout = ULONG_MAX) | 获取 num 个资源,直到超时。 | num :需要获取的资源数量timeout :超时时间(毫秒) | bool :成功获取资源返回 true ,超时返回 false |
检查方法 | |||
tryAcquire(int num = 1) | 尝试获取 num 个资源,若资源不足则立即返回。 | num :需要获取的资源数量 | bool :成功获取资源返回 true ,否则返回 false |
available() const | 返回可用资源的数量。 | int :可用资源数量 | |
其他方法 | |||
setCount(int count) | 设置信号量的计数器值。 | count :新的计数值 | 无 |
maximumCount() const | 返回信号量的最大值。 | int :最大值 | |
currentCount() const | 返回当前信号量的计数值。 | int :当前计数值 |
#include <QCoreApplication> // 引入Qt核心应用程序模块 #include <QThread> // 引入Qt线程模块 #include <QSemaphore> // 引入Qt信号量模块 #include <QDebug> // 引入Qt调试输出模块 // 创建一个信号量,初始计数为3,表示最多同时允许3个线程访问资源 QSemaphore semaphore(3); class Worker : public QThread { public: void run() override { // 重写QThread的run()方法,定义线程执行的代码 // 尝试在1000毫秒内获取一个资源 if (semaphore.tryAcquire(1, 1000)) { // 尝试获取一个资源,如果在超时内未能获取,则返回false qDebug() << "Thread" << QThread::currentThreadId() << "acquired a resource."; // 打印线程获取资源的消息 QThread::sleep(2); // 模拟工作,线程休眠2秒 qDebug() << "Thread" << QThread::currentThreadId() << "finished working."; // 打印线程完成工作的消息 semaphore.release(); // 释放一个资源,使其他等待的线程可以获取到资源 } else { qDebug() << "Thread" << QThread::currentThreadId() << "could not acquire a resource within timeout."; // 打印超时未获取资源的消息 } // 显示信号量的状态 qDebug() << "Current available resources:" << semaphore.available(); // 打印当前可用资源的数量 semaphore.setCount(5); // 修改信号量的计数器值为5,增加可用资源 qDebug() << "Max count:" << semaphore.maximumCount(); // 打印信号量的最大计数值 qDebug() << "Current count:" << semaphore.currentCount(); // 打印当前信号量的计数值 } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建Qt应用程序实例 // 创建多个Worker线程实例 Worker worker1, worker2, worker3, worker4, worker5; // 启动线程 worker1.start(); worker2.start(); worker3.start(); worker4.start(); worker5.start(); // 等待所有线程完成 worker1.wait(); worker2.wait(); worker3.wait(); worker4.wait(); worker5.wait(); return a.exec(); // 进入Qt事件循环 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。