Results 1 to 2 of 2

Thread: moveToThread() Problem

  1. #1

    Default moveToThread() Problem

    Hello,

    Im having some problems related to thread affinity.

    Basically I create some worker threads from my main thread as follows:

    Qt Code:
    1. threadPool[i]->start();
    2. threadPool[i]->moveToThread(threadPool[i]);
    To copy to clipboard, switch view to plain text mode 

    I perform the moveToThread() to ensure that objects instantiated inside the threads run() method are in the new thread and not the main thread context (this is a problem if i dont moveToThread()).

    This all works perfectly, until I shut the threads down. When this happens I get some very strange errors (segfaults mostly). I think this is because when the threads shut down, the thread object is now living in a non-existent thread.

    Please could someone give me some idea of how to avoid this problem? My requirements are simply that everything in the threads run() function are created/executed under that threads context as opposed to the parent thread. moveToThread() achieves this, but introduces problems when the thread closes itself, because once closed the thread its supposed to live in a thread which no longer exists.

    The threads run() method is as followed.
    Qt Code:
    1. void NThread::run()
    2. {
    3. qDebug()<<"[Starting Thread] : "<<QThread::currentThreadId();
    4.  
    5. setup();
    6.  
    7. // enter event loop
    8. exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: moveToThread() Problem

    Objects created inside run() are always associated with the new thread, you don't need to move the thread object itself to the new thread for that.

    If your objects crash after the thread ends then there is something wrong with them. If you return from exec(), events are not handled anymore so the thread object can't crash because none of its events will be executed as it is not associated with any event loop. And by the way, you can always push the thread object back to the main thread after exec() returns but I doubt it will solve your problem. It's probably elsewhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 19
    Last Post: 3rd April 2009, 23:17
  2. deployment problem: msvc++ 2008 Express, Qt 4.4.3
    By vonCZ in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2008, 14:38
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.