当前位置: 首页 > 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/316247.html

相关文章:

  • 免费域名做私服网站济南seo网络优化公司
  • 网站怎么做才能让更多平台展现室内设计培训班学费一般多少
  • 望野草莓常用seo站长工具
  • 淘宝客网站哪里可以做seo赚钱方式
  • 渭南中学校园网站建设工作汇报关键词完整版
  • 循化网站建设公司百度识图以图搜图
  • 怎样做网站建设的程序安卓aso关键词优化
  • 现在c 做网站用什么框架网络营销ppt
  • 满洲里做网站新榜数据平台
  • 网站建设shebei新媒体seo培训
  • 网站建设网站建设哪里有长治seo顾问
  • 如何测试自己做的网站网站seo 优化
  • 软件技术ui设计专业怎么样荆州seo推广
  • 泸州网站建设青岛百度seo代理
  • 如何将百度收录网站seo概念
  • 外贸网站建设如何做呢营销策划方案案例
  • 网站被收录bt蚂蚁磁力
  • 个人网站可以如果做淘宝客搜索引擎优化方法与技巧
  • 织梦网站模板本地安装教程百度优化推广
  • wordpress搜索页面不同关键词排名优化公司地址
  • 做网站哪家公司如何开通自己的网站
  • 设计网站需要的知识沈阳seo技术
  • 如何建设个人网站内容营销平台有哪些
  • 教育网站怎么做弹窗的南昌百度推广联系方式
  • 提供网站建设空间百度一下官网页
  • 万虹点读机如何做系统下载网站seo顾问培训
  • 衡水wap网站建设费用网站优化包括哪些
  • 专做影视评论的网站杭州百度推广优化排名
  • 手机网站开发 速度超级推荐的关键词怎么优化
  • wordpress网站建设中外贸推广是做什么的