-
Notifications
You must be signed in to change notification settings - Fork 2
/
asynchronouszimreader.cpp
42 lines (33 loc) · 1.31 KB
/
asynchronouszimreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <QMetaType>
#include <QDebug>
#include "asynchronouszimreader.h"
AsynchronousZimReader::AsynchronousZimReader(QObject *parent, ZimFileWrapper *zimFileWrapper) :
QThread(parent), zimFileWrapper(zimFileWrapper), m_ready(false)
{
qDebug() << Q_FUNC_INFO << QThread::currentThreadId(); // Main Thread
// Start the worker thread
start();
// Wait for the worker thread to be ready;
while(!m_ready) msleep(50);
}
void AsynchronousZimReader::readAsync(const QUrl &url)
{
qDebug() << Q_FUNC_INFO << QThread::currentThreadId(); // Main Thread
emit readRequested(url);
}
void AsynchronousZimReader::run()
{
qDebug() << Q_FUNC_INFO << QThread::currentThreadId(); // Worker Thread
// This QObject lives in the worker thread
AsynchronousZimReaderWorker worker(0, zimFileWrapper); // DO NOT define 'this' pointer as parent
// Pass read requests to AsynchronousZimReaderWorker in the worker thread
connect(this, SIGNAL(readRequested(QUrl)),
&worker, SLOT(readFromZimFile(QUrl))/*, Qt::QueuedConnection*/);
// Forward the signal to the clients
connect(&worker, SIGNAL(readDone(QByteArray, QString)), this,
SIGNAL(readDone(QByteArray, QString))/*, Qt::QueuedConnection*/);
// Mark the worker thread as ready
m_ready = true;
// Event loop (necessary to process signals)
exec();
}