当前位置: 首页 > wzjs >正文

网站建设制作开发 小程序开发定制 软件系统开发深圳网站建设方案

网站建设制作开发 小程序开发定制 软件系统开发,深圳网站建设方案,做视频上传可以赚钱的网站,国际版网站可以在国内做推广吗信号量 【C并发编程】(八)信号量 QT中的信号量类是QSemaphore,用法与C标准中的std::counting_semaphore类似。不同的是, QSemaphore无法指定最大计数。为了限定最大计数,可以采用两个QSemaphore信号量。下面使用一个…

信号量

【C++并发编程】(八)信号量

QT中的信号量类是QSemaphore,用法与C++标准中的std::counting_semaphore类似。不同的是, QSemaphore无法指定最大计数。为了限定最大计数,可以采用两个QSemaphore信号量。下面使用一个生成者-消费者例子展示QSemaphore的用法:

https://github.com/BinaryAI-1024/QtStudy/tree/master/thread/semaphore

// main.cpp
#include <QCoreApplication>
#include <QThread>
#include "producer.h"
#include "consumer.h"// 定义全局变量
const int BufferSize = 5;
QSemaphore emptySlots(BufferSize); // 管理缓冲区的空闲空间
QSemaphore fullSlots(0);           // 管理缓冲区的已用空间
int buffer[BufferSize];  int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);// 创建线程QThread producerThread, consumerThread;// 创建生产者和消费者对象Producer producer;Consumer consumer;// 将对象移动到线程producer.moveToThread(&producerThread);consumer.moveToThread(&consumerThread);// 连接线程启动信号QObject::connect(&producerThread, &QThread::started, &producer, &Producer::produce);QObject::connect(&consumerThread, &QThread::started, &consumer, &Consumer::consume);// 启动线程producerThread.start();consumerThread.start();return a.exec();
}
// consumer.h
#ifndef CONSUMER_H
#define CONSUMER_H#include <QObject>
#include <QSemaphore>// main.cpp中的全局变量
extern QSemaphore emptySlots;
extern QSemaphore fullSlots;
extern int buffer[];
extern const int BufferSize;  // 添加 BufferSize 声明class Consumer : public QObject {Q_OBJECT
public:explicit Consumer(QObject *parent = nullptr);public slots:void consume();
};#endif // CONSUMER_H
//consumer.cpp
#include "consumer.h"
#include <QDebug>
#include <QThread>Consumer::Consumer(QObject *parent) : QObject(parent) {}void Consumer::consume() {for (int i = 0; i < 10; ++i) {fullSlots.acquire();  // 等待直到有可消费的数据,如果有fullSlots计数-1int data = buffer[i % BufferSize];  // 现在 BufferSize 可用qDebug() << "Consumed:" << data << "by thread" << QThread::currentThread();emptySlots.release(); // 释放空槽位emptySlots计数+1QThread::msleep(1000);}
}
//producer.h
#ifndef PRODUCER_H
#define PRODUCER_H#include <QObject>
#include <QSemaphore>// main.cpp中的全局变量
extern QSemaphore emptySlots;
extern QSemaphore fullSlots;
extern int buffer[];
extern const int BufferSize;class Producer : public QObject {Q_OBJECT
public:explicit Producer(QObject *parent = nullptr);public slots:void produce();
};#endif // PRODUCER_H
//producer.cpp
#include "producer.h"
#include <QDebug>
#include <QThread>Producer::Producer(QObject *parent) : QObject(parent) {}void Producer::produce() {for (int i = 0; i < 10; ++i) {emptySlots.acquire();  //  等待,直到缓冲区有空位,如果有emptySlots计数-1buffer[i % BufferSize] = i;qDebug() << "Produced:" << i << "by thread" << QThread::currentThread();fullSlots.release();   // 生产了一个数据:fullSlots计数+1QThread::msleep(500);}
}

emptySlots.acquire() 确保生产者生产的数据不会超过缓冲区大小。

fullSlots.acquire() 确保消费者不会读取空数据。

emptySlots.release()fullSlots.release() 保持生产消费的平衡。

emptySlotsfullSlots的计数有一个加一就有一个减一,保证了emptySlots计数+fullSlots计数=BuffferSize。这确保了生产者生产的数据不会超过缓冲区的大小。

结果:

Produced: 0 by thread QThread(0x77fe98)
Consumed: 0 by thread QThread(0x77fe90)
Produced: 1 by thread QThread(0x77fe98)
Produced: 2 by thread QThread(0x77fe98)
Consumed: 1 by thread QThread(0x77fe90)
Produced: 3 by thread QThread(0x77fe98)
Consumed: 2 by thread QThread(0x77fe90)
Produced: 4 by thread QThread(0x77fe98)
Produced: 5 by thread QThread(0x77fe98)
Consumed: 3 by thread QThread(0x77fe90)
Produced: 6 by thread QThread(0x77fe98)
Produced: 7 by thread QThread(0x77fe98)
Consumed: 4 by thread QThread(0x77fe90)
Produced: 8 by thread QThread(0x77fe98)
Produced: 9 by thread QThread(0x77fe98)
Consumed: 5 by thread QThread(0x77fe90)
Consumed: 6 by thread QThread(0x77fe90)
Consumed: 7 by thread QThread(0x77fe90)
Consumed: 8 by thread QThread(0x77fe90)
Consumed: 9 by thread QThread(0x77fe90)

程序中设置了消费者取走数据的速度比生产者慢,因此可以看到 buffer 被逐步填满,之后再被逐步消费的情况。

http://www.dtcms.com/wzjs/89282.html

相关文章:

  • 物流网站建设规划书seo经验
  • 手机网站建设规划书中国重大新闻
  • 网站的空间seo沈阳
  • 帮企网站建设代运营seo引擎搜索网站关键词
  • 如何做招聘网站效果分析400个成品短视频
  • 为什么两学一做进不去网站网站报价
  • wordpress lamp临沂seo公司
  • 有一个做场景动画的网站分析网站
  • 江西企业网站定制百度注册网站怎么弄
  • 设计网站名称网站建设服务公司
  • 南京网站建设价格中小企业网络推广
  • 建设信用中国网站西昌seo快速排名
  • 西安手机网站建设动力无限市场营销一般在哪上班
  • 南宁北京网站建设国内哪个搜索引擎最好用
  • 厦门快速建网站互联网服务平台
  • 网站开发语言和数据库网络营销工程师是做什么的
  • 服务 好的网站制作搜索引擎的工作原理是什么?
  • 搜狗推广手机客户端百度推广账户优化
  • 微信扫一扫登录网站如何做免费网站谁有靠谱的
  • 网站开发税费域名查询平台
  • 专门做冷门旅行的网站新闻最近的新闻
  • 如何网站备案软文营销的技巧
  • 网站运营做网页设计在线一键免费生成网页网站
  • 旅游网站经营模式餐饮营销引流都有什么方法
  • 深圳找个人做网站鸣蝉智能建站
  • 网页制作及网站设计昆山网站建设
  • 自己给公司做网站该怎么做b2b网站
  • 临夏市建设局网站买卖交易网
  • 河南周口东宇网站建设产品关键词
  • wordpress怎么修改语言seo排名优化公司哪家好