2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-02-11 17:26:46 +00:00
|
|
|
// Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
|
2022-05-10 10:06:48 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2022-02-11 17:26:46 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QWaitCondition>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
//! [0]
|
2022-02-11 17:26:46 +00:00
|
|
|
constexpr int DataSize = 100000;
|
|
|
|
constexpr int BufferSize = 8192;
|
2011-04-27 17:16:41 +00:00
|
|
|
|
2022-12-15 00:06:43 +00:00
|
|
|
QMutex mutex; // protects the buffer and the counter
|
2011-04-27 10:05:43 +00:00
|
|
|
char buffer[BufferSize];
|
2022-12-15 00:06:43 +00:00
|
|
|
int numUsedBytes;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QWaitCondition bufferNotEmpty;
|
|
|
|
QWaitCondition bufferNotFull;
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
class Producer : public QThread
|
|
|
|
//! [1] //! [2]
|
|
|
|
{
|
|
|
|
public:
|
2022-02-11 17:26:46 +00:00
|
|
|
explicit Producer(QObject *parent = nullptr)
|
|
|
|
: QThread(parent)
|
2011-04-27 17:16:41 +00:00
|
|
|
{
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2022-02-11 17:26:46 +00:00
|
|
|
private:
|
2016-06-15 08:12:35 +00:00
|
|
|
void run() override
|
2011-04-27 17:16:41 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < DataSize; ++i) {
|
2022-02-11 17:26:46 +00:00
|
|
|
{
|
|
|
|
const QMutexLocker locker(&mutex);
|
2022-12-15 00:06:43 +00:00
|
|
|
while (numUsedBytes == BufferSize)
|
2022-02-11 17:26:46 +00:00
|
|
|
bufferNotFull.wait(&mutex);
|
|
|
|
}
|
2011-04-27 17:16:41 +00:00
|
|
|
|
2017-04-14 04:13:52 +00:00
|
|
|
buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)];
|
2011-04-27 17:16:41 +00:00
|
|
|
|
2022-12-15 00:06:43 +00:00
|
|
|
{
|
|
|
|
const QMutexLocker locker(&mutex);
|
|
|
|
++numUsedBytes;
|
|
|
|
bufferNotEmpty.wakeAll();
|
|
|
|
}
|
2011-04-27 17:16:41 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2011-04-27 17:16:41 +00:00
|
|
|
};
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
class Consumer : public QThread
|
|
|
|
//! [3] //! [4]
|
|
|
|
{
|
|
|
|
public:
|
2022-02-11 17:26:46 +00:00
|
|
|
explicit Consumer(QObject *parent = nullptr)
|
|
|
|
: QThread(parent)
|
2011-04-27 17:16:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:26:46 +00:00
|
|
|
private:
|
2016-06-15 08:12:35 +00:00
|
|
|
void run() override
|
2011-04-27 17:16:41 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < DataSize; ++i) {
|
2022-02-11 17:26:46 +00:00
|
|
|
{
|
|
|
|
const QMutexLocker locker(&mutex);
|
2022-12-15 00:06:43 +00:00
|
|
|
while (numUsedBytes == 0)
|
2022-02-11 17:26:46 +00:00
|
|
|
bufferNotEmpty.wait(&mutex);
|
|
|
|
}
|
2011-04-27 17:16:41 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "%c", buffer[i % BufferSize]);
|
|
|
|
|
2022-12-15 00:06:43 +00:00
|
|
|
{
|
|
|
|
const QMutexLocker locker(&mutex);
|
|
|
|
--numUsedBytes;
|
|
|
|
bufferNotFull.wakeAll();
|
|
|
|
}
|
2011-04-27 17:16:41 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
2011-04-27 17:16:41 +00:00
|
|
|
//! [4]
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
//! [5]
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
//! [5] //! [6]
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
Producer producer;
|
|
|
|
Consumer consumer;
|
|
|
|
producer.start();
|
|
|
|
consumer.start();
|
|
|
|
producer.wait();
|
|
|
|
consumer.wait();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//! [6]
|
2011-04-27 17:16:41 +00:00
|
|
|
|