I wonder is it thread safe to pass another objects to the object that runs in another thread than GUI thread. I mean by that is it thread safe to do something like that? What is happening with this example object through the lifetime of the app?
In worker.h file
private:
Example* example;
In worker.c file
Worker::Worker(QObject *parent, Example *example) :
QObject(parent),
example(example)
{
}
In main.c
Example example(nullptr);
auto worker = new Worker(nullptr, &example);
auto thread = new QThread;
Printer printer;
QObject::connect(thread, &QThread::started, worker, &Worker::doWork);
QObject::connect(worker, &Worker::readedText, &printer, &Printer::printMessage);
QObject::connect(worker, &Worker::workDone, thread, &QThread::quit);
QObject::connect(thread, &QThread::finished, worker, &Worker::deleteLater);
worker->moveToThread(thread);
thread->start();
And what if GUI thread would have to use an Example object since i'm passing the pointer to that object?
Aucun commentaire:
Enregistrer un commentaire