Qt笔记——QMutex&QWaitCondition处理生产者消费者模式

QSemaphore处理生产者/消费者模式:

https://blog.csdn.net/qq_41895747/article/details/104102307

 

main.cpp

#include 
#include 
#include 
#include 
#include 

const int DataSize = 1000;//数据区大小
const int BufferSize = 80;//缓冲区大小

int buffer[BufferSize];
QWaitCondition bufferEmpty;
QWaitCondition bufferFull;
QMutex mutex;//锁定的互斥量,使用互斥量保证操作的原子性
int numUseBytes=0;//可用字节
int rIndex=0;//当前读取缓冲区的位置

//消费者
class Producer:public QThread{
public:
    Producer();
    void run();
};

Producer::Producer(){

}

void Producer::run(){
    for(int i=0;i

.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = WaitCondition
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

运行效果

Qt笔记——QMutex&QWaitCondition处理生产者消费者模式_第1张图片

 

你可能感兴趣的:(Qt开发)