c++ - I'm not sure why my QThread pattern is blocking -


i've seen many posts , articles on qthread , moving qobjects between qthreads alas, still causing me headaches. pattern i'm trying adopt:

#include "connectionthread.h" #include <cassert>  connectionthread::connectionthread(connectionptr const &connectionptr) :                                worker(null),                                m_connectionptr(connectionptr) {     connect(this, signal(executesignal()), this, slot(loginprocess())); }  void connectionthread::start() {     if(worker) {         if(worker->isrunning()) {             worker->quit();         }         delete worker;     }     worker = new qthread;     connect(worker, signal(started()), this, slot(run()));     worker->start(); }  void connectionthread::run() {     emit executesignal(); }  void connectionthread::loginprocess() {     m_connectionptr->connect(); } 

now instance of created in main gui thread, yet when loginprocess called, blocks until completion causes app's gui hang. note, no difference observed if put logic code directly run function , omit signal follows:-

void connectionthread::run()  {     m_connectionptr->connect(); } 

so assumed need move 'this' thread named worker, like:

void connectionthread::start() {     if(worker) {         if(worker->isrunning()) {             worker->quit();         }         delete worker;     }     worker = new qthread;     this->movetothread(worker);     connect(worker, signal(started()), this, slot(run()));     worker->start(); } 

but gives me

qobject: cannot create children parent in different thread. 

i'm not sure why case since instance of connectionthread created , start function called thread. let's call other thread guithread. means guithread has control should able transfer ownership of connectionthread instance worker thread.

one final possibility haven't explored yet possibility of moving m_connectionptr worker thread..

any thoughts on above pattern, how might improve it, , how can prevent blocking?

edit 1: following proposed solution doesn't work expected because finished() signal never being emitted worker

edit 2: fixed finished signal being triggered still can't move m_connectionptr main thread within moveconnectionptrback. gives error "qobject::movetothread: current thread (0x102900380) not object's thread (0x10493b740). cannot move target thread (0x102900380)"

so, think i've figured out do: solution seems transfer thread ownership of connectionptr worker thread:

#include "connectionthread.h"  connectionthread::connectionthread(connectionptr const &connectionptr) :                                worker(null),                                m_connectionptr(connectionptr) {     // edit 2 added bit -- m_connectionptr sends signal when work finished     connect(m_connectionptr.data(),              signal(connectfinishedsignal()), this, slot(quitthread())); }  void connectionthread::start() {     if(worker) {         if(worker->isrunning()) {             worker->quit();         }         delete worker;     }     worker = new qthread;     m_connectionptr->movetothread(worker);     connect(worker, signal(started()), m_connectionptr.data(), slot(connect()));     connect(worker, signal(finished()), this, slot(moveconnectionptrback()));     worker->start(); }  void connectionthread::moveconnectionptrback() {     // call failing still     m_connectionptr->movetothread(qapplication::instance()->thread()); }  // edit 2 added bit; quitting causes worker send finished signal() causes // correct triggering of moveconnectionptrback() function void connectionthread::quitthread() {     worker->quit(); } 

(note m_connectionptr shared ptr 'connection' derived qobject has no parent; likewise connectionthread derived qobject again, has no parent).

since m_connectionptr used other threads in future, had move again main thread shown moveconnectionptrback slot.

seems trick overall not entirely bug-free.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -