qt5base-lts/examples/corelib/threads/waitconditions/waitconditions.cpp
Giuseppe D'Angelo fddeec60cb Wait conditions example: fix an incorrect condition variable usage
3a449bbb69 amended the code to remove
acquiring a lock when waking up a condition variable. It is fine to
not have a lock associated when waking a condition variable; what I
misunderstood was the scope of the lock, which (and this underlines
the importance of commenting _what exactly_ a lock protects, for
each and ever lock) protected both the buffer as well as the counter
of the buffer. This made my reasoning flawed: it is necessary to keep
the lock while notifying, otherwise the counterpart could verify the
condition isn't satisfied and wait (e.g. see numUsedBytes==0), missing
the wake from the other thread (which could arrive between the check and
the wait).

Amends the previous commit.

Change-Id: If7db2d045331f1b33b976fb6bf6aa9117c41678f
Pick-to: 5.15 6.2 6.4 6.5
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
2022-12-28 15:10:40 +01:00

109 lines
2.3 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QCoreApplication>
#include <QMutex>
#include <QMutexLocker>
#include <QObject>
#include <QRandomGenerator>
#include <QThread>
#include <QWaitCondition>
#include <stdio.h>
#include <stdlib.h>
//! [0]
constexpr int DataSize = 100000;
constexpr int BufferSize = 8192;
QMutex mutex; // protects the buffer and the counter
char buffer[BufferSize];
int numUsedBytes;
QWaitCondition bufferNotEmpty;
QWaitCondition bufferNotFull;
//! [0]
//! [1]
class Producer : public QThread
//! [1] //! [2]
{
public:
explicit Producer(QObject *parent = nullptr)
: QThread(parent)
{
}
private:
void run() override
{
for (int i = 0; i < DataSize; ++i) {
{
const QMutexLocker locker(&mutex);
while (numUsedBytes == BufferSize)
bufferNotFull.wait(&mutex);
}
buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)];
{
const QMutexLocker locker(&mutex);
++numUsedBytes;
bufferNotEmpty.wakeAll();
}
}
}
};
//! [2]
//! [3]
class Consumer : public QThread
//! [3] //! [4]
{
public:
explicit Consumer(QObject *parent = nullptr)
: QThread(parent)
{
}
private:
void run() override
{
for (int i = 0; i < DataSize; ++i) {
{
const QMutexLocker locker(&mutex);
while (numUsedBytes == 0)
bufferNotEmpty.wait(&mutex);
}
fprintf(stderr, "%c", buffer[i % BufferSize]);
{
const QMutexLocker locker(&mutex);
--numUsedBytes;
bufferNotFull.wakeAll();
}
}
fprintf(stderr, "\n");
}
};
//! [4]
//! [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]