What would be the optimal way of reading from a QFile
that resides in a different thread than the one I want to read from?
Consider:
class AFile : public QObject
{
Q_OBJECT
public:
AFile(const QString &name, QObject *parent = Q_NULLPTR) : QObject(parent), m_File(name) { m_File.open(QIODevice::ReadWrite); }
public slots:
void write(const QByteArray &data, qint64 pos) { m_File.seek(pos); m_File.write(data); }
private:
mutable QFile m_File;
};
class AData : public QObject
{
Q_OBJECT
public:
using QObject::QObject;
void save(const QByteArray &data) { emit saveData(data, 0); }
signals:
void saveData(const QByteArray &data, qint64 pos) const;
};
AFile file("myfile");
AData data;
QThread *thread = new QThread;
connect(&data, &AData::saveData, &file, &AFile::write);
file.moveToThread(&thread);
thread.start();
data.save("Some data");
//how to concurrently read though?
AFile
is a wrapper around QFile
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.
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).A test UI demonstrates how to use it: