[Qt-interest] [[Fwd: Re: QThread::run() running in wrong thread]]

Ove Fagerheim ove.fagerheim at nax.no
Sun May 23 18:22:39 CEST 2010


Sorry 'bout that, you're right of course. I took one shortcut to many. 

I have edited the code below to more accurately mirror the program. Also
added the error message received when trying to move object back to main
thread.


---------Videresendt melding-------
Fra: Thiago Macieira <thiago at kde.org>
Til: qt-interest at trolltech.com
Emne: Re: [Qt-interest] [Fwd: Re: QThread::run() running in wrong
thread]
Dato: Sun, 23 May 2010 18:05:32 +0200

Em Domingo 23. Maio 2010, às 17.08.30, Ove Fagerheim escreveu:
> Seems like th code really is running in main thread after all. I cant
> change affinity for m_worker in workerthread, due to wrong thread
> running.
> 
> class SomeObject;
> 
> class WorkerThread : public QThread
>  {
>    .
>    create(SomeObject *pParam, QObject *pParent) {
>       m_worker = pParam;
>       m_parent = pParent;
>    }
>    .
>    void run() {
>       // Next statement still reports wrong thread
>       fprintf(stderr, "MyThread is %p\n", currentThread(); }
>       // do some work>       
        // next statement failes due to code running in wrong thread error:
        // "Current thread (0x842800) is not the object's thread (0x6a4210).
        // Cannot move to target thread (0x6a4210)"
        // 0x6a4210 is the main thread.
        m_worker->moveToThread(m_parent->thread());
>    }
> 
>    QObject *m_parent;
>    SomeObject *m_worker;
>  };
> 
  class MainObject() : public QObject
{
   void Work()
   {
     SomeObject * p = new SomeObject();
     WorkerThread *thr = new WorkerThread();
     p->moveToThread(thr);
     thr->create(p, this);
     thr->start();
   }
}

> int main(int argc, char *argv[])
> {
     MainObject *obj = new MainObject();
     obj->Work();
> }

I can't help you. Your code doesn't compile (use of "this" in main).

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list