Resurrection Resurrection - 1 year ago 132
C++ Question

Reading from a QFile in different thread

What would be the optimal way of reading from a

that resides in a different thread than the one I want to read from?


class AFile : public QObject
AFile(const QString &name, QObject *parent = Q_NULLPTR) : QObject(parent), m_File(name) {; }

public slots:
void write(const QByteArray &data, qint64 pos) {; m_File.write(data); }

mutable QFile m_File;

class AData : public QObject
using QObject::QObject;

void save(const QByteArray &data) { emit saveData(data, 0); }

void saveData(const QByteArray &data, qint64 pos) const;

AFile file("myfile");
AData data;
QThread *thread = new QThread;

connect(&data, &AData::saveData, &file, &AFile::write);

thread.start();"Some data");

//how to concurrently read though?

is a wrapper around
that handles all writes to the file. It is moved to a different thread in order to not slow the main thread with expensive disk write operations. Multi-thraeded read situations are handled by locks or mutex but that would defeat the purpose of having the file in the different thread in the first place since the main one (wanting to read from it) will have to wait for the write to finish so in such a case I may leave it in the main thread to begin with.

The option I do not quite like are signals and slots because it seems to me a bit heavy weight. I would basically send a request for the data and wait for it to be read (either returning the data via signal or sending variable to be filled with the data and waiting for a signal it has been finished).

After some consideration this seems to be the best approach but I am not really sure it is a good design.

Answer Source

Concurrent reading is done by splitting the request and indication parts: you first request the data, the reader reads it, then you react to the indication that a data was read.

Since the file object is accessed from a worker thread, I've encapsulated it inside the AData class. You can move the class to a worker thread, the signals you call from outside are thread-safe.

It should also be possible to make an asynchronous wrapper around a QFile, but doing it properly would require using Qt's implementation details (core_private module).

#include <QtWidgets>

class AData : public QObject
    QFile m_file;
    explicit AData(QObject * parent = nullptr) : QObject{parent} {
        connect(this, &AData::save, this, [=](const QByteArray & data, qint64 pos){
        connect(this, &AData::load, this, [=](qint64 pos, qint64 len){
           if (len == -1) len = m_file.size();
           auto data =;
           emit loaded(data, pos);
    bool open(const QString & name) {
    Q_SIGNAL void save(const QByteArray &data, qint64 pos = 0) const;
    Q_SIGNAL void load(qint64 pos, qint64 len) const;
    Q_SIGNAL void loaded(const QByteArray &data, qint64 pos) const;

A test UI demonstrates how to use it:

int main(int argc, char ** argv) {
    QApplication app{argc, argv};
    struct Thread : QThread {
        ~Thread() { quit(); wait(); }
    } ioThread;
    AData data;"myfile");

    QWidget ui;
    QGridLayout layout{&ui};
    QTextEdit text;
    QPushButton load{"Load"};
    QPushButton save{"Save"};
    QPushButton clear{"Clear"};
    layout.addWidget(&text, 0, 0, 1, 2);
    layout.addWidget(&load, 1, 0);
    layout.addWidget(&save, 1, 1);
    layout.addWidget(&clear, 2, 0, 1, 2);;

    using Q = QObject;
    Q::connect(&load, &QPushButton::clicked, &data, [&]{
        data.load(0, -1);
    Q::connect(&data, &AData::loaded, &app, [&](const QByteArray & data, qint64){
    Q::connect(&save, &QPushButton::clicked, &data, [&]{>toPlainText().toUtf8());
    Q::connect(&clear, &QPushButton::clicked, &text, &QTextEdit::clear);

    return app.exec();
#include "main.moc"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download