QT多线程(信号量QSemaphore的使用)

信号量QSemaphore的使用

程序

信号量可以理解为对互斥量功能的扩展,互斥量只能锁定一次而信号量可以获取多次,它可以用来保护一定数量的同种资源。信号量的典型用例是控制生产者/消费者之间共享的环形缓冲区。
生产者/消费者实例中对同步的需求有两处:
(1)如果生产者过快地生产数据,将会覆盖消费者还没有读取的数据。
(2)如果消费者过快地读取数据,将越过生产者并且读取到一些过期数据。
QSemaphore可使生产者和消费者线程同时分别操作缓冲区的不同部分,这是一种比较高效的方法。

main.cpp
#include <QCoreApplication>
#include <QSemaphore>
#include <QThread>
#include <stdio.h>

const int DataSize=1000;
const int BufferSize=80;
int buffer[BufferSize];
QSemaphore freeBytes(BufferSize);//生产者可填充的缓冲区
QSemaphore usedBytes(0
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 `QSemaphore` 实现多线程加载图片的示例: ```cpp #include <QApplication> #include <QWidget> #include <QVBoxLayout> #include <QHBoxLayout> #include <QLabel> #include <QImage> #include <QPixmap> #include <QFile> #include <QSemaphore> #include <QThread> #include <QDebug> const int kNumThreads = 4; // 线程数量 const int kMaxImages = 8; // 最大图片数量 class ImageLoader : public QObject { Q_OBJECT public: ImageLoader(QSemaphore *semaphore, const QString &filename, QLabel *label, QObject *parent = nullptr) : QObject(parent), semaphore_(semaphore), filename_(filename), label_(label) {} signals: void finished(); public slots: void load() { QFile file(filename_); if (file.open(QIODevice::ReadOnly)) { QImage image; if (image.load(&file)) { QPixmap pixmap = QPixmap::fromImage(image); emit imageLoaded(pixmap); } } file.close(); emit finished(); semaphore_->release(); } void setImage(const QPixmap &pixmap) { label_->setPixmap(pixmap); } private: QSemaphore *semaphore_; QString filename_; QLabel *label_; }; class ImageLoaderThread : public QThread { Q_OBJECT public: ImageLoaderThread(QSemaphore *semaphore, const QString &filename, QLabel *label, QObject *parent = nullptr) : QThread(parent), semaphore_(semaphore), filename_(filename), label_(label) {} signals: void imageLoaded(const QPixmap &pixmap); protected: void run() override { ImageLoader loader(semaphore_, filename_, label_); connect(&loader, &ImageLoader::imageLoaded, this, &ImageLoaderThread::imageLoaded); connect(&loader, &ImageLoader::finished, this, &ImageLoaderThread::quit); loader.load(); } private: QSemaphore *semaphore_; QString filename_; QLabel *label_; }; class MainWindow : public QWidget { public: MainWindow(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(this); for (int i = 0; i < kMaxImages; ++i) { QLabel *label = new QLabel(this); label->setFixedSize(200, 200); layout->addWidget(label); labels_.append(label); } QThread::currentThread()->setObjectName("Main"); } ~MainWindow() { for (auto thread : threads_) { thread->quit(); thread->wait(); delete thread; } } void startLoading() { // 初始化信号量 semaphore_.release(kMaxImages); // 启动线程 for (int i = 0; i < kNumThreads; ++i) { ImageLoaderThread *thread = new ImageLoaderThread(&semaphore_, QString(":/images/%1.png").arg(i + 1), labels_.at(i)); threads_.append(thread); connect(thread, &ImageLoaderThread::imageLoaded, this, &MainWindow::onImageLoaded, Qt::QueuedConnection); thread->start(); } } public slots: void onImageLoaded(const QPixmap &pixmap) { // 查找空闲的标签 int index = -1; for (int i = 0; i < kMaxImages; ++i) { if (labels_.at(i)->pixmap() == nullptr) { index = i; break; } } // 设置图片 if (index >= 0 && index < kMaxImages) { labels_.at(index)->setPixmap(pixmap); } } private: QList<QLabel *> labels_; QList<ImageLoaderThread *> threads_; QSemaphore semaphore_; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); window.startLoading(); return app.exec(); } #include "main.moc" ``` 在这个例子中,我们创建了一个 `ImageLoader` 类,负责从文件中加载图片,并将加载完成的图片转换成 `QPixmap` 类型,然后通过信号将图片传递给线程。`ImageLoaderThread` 类继承自 `QThread`,负责在子线程中创建 `ImageLoader` 对象,并连接信号槽,将加载完成的图片发送给主线程。主线程中创建了多个 `ImageLoaderThread` 对象,并启动线程,等待图片加载完成后将图片显示在 QLabel 控件中。为了控制最大并发数,我们使用了一个 `QSemaphore` 对象,每个线程在加载图片前申请一个信号量,加载完成后释放信号量。如果信号量已经全部被占用,则线程会阻塞在 `acquire()` 函数处,等待有空闲的信号量。这样就可以限制线程的并发数量,防止过多的线程同时加载图片导致系统资源耗尽。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值