Results 1 to 12 of 12

Thread: QThread: Move owner of thread to thread with moveToThread

  1. #1
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread: Move owner of thread to thread with moveToThread

    Hi,
    is it ok to move the object owning the thread to the thread? Like in the example below.

    a.h:
    QThread* m_pThread;

    a.cpp:
    m_pThread = new QThread;
    this->moveToThread(m_pThread);

    Thanks!

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QThread: Move owner of thread to thread with moveToThread

    I would be _very_ surprised if it would be ok.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    Quote Originally Posted by SepticInsect View Post
    Hi,
    is it ok to move the object owning the thread to the thread?
    In your example the QThread object is not "owned" by anyone (and you have to delete it to avoid a memory leak). Depending on the rest of the code (which we do not have access to), it may or may not be correct. However the design looks bad: if the QThread is only meant to be used internally by the class you define, then it should not affect the object's public behavioural properties such as its thread affinity.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QThread: Move owner of thread to thread with moveToThread

    What are you hoping to achieve by this? I cannot see it will end well at delete time with the parent QObject in one thread and the QThread in another.

    If you have convinced yourself that your program really needs threads (most don't) then this is a good way to do QThread use and cleanup:
    http://mayaposch.wordpress.com/2011/...l-explanation/

  5. The following user says thank you to ChrisW67 for this useful post:

    SepticInsect (10th September 2012)

  6. #5
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    Thanks for the help! I will try the example in the link you sent.

  7. #6
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    I have implemented my thread according to: http://mayaposch.wordpress.com/2011/...l-explanation/ and it works fine.
    If I would like to stop my thread from the main thread (when the user presses Cancel) how shall I do that?

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QThread: Move owner of thread to thread with moveToThread

    It depends on what your thread is doing, but something like:
    Qt Code:
    1. class Worker : public QObject {
    2. Q_OBJECT
    3. public:
    4. Worker(): stopRequested(false) { }
    5. ~Worker() { }
    6.  
    7. public slots:
    8. void process() {
    9. while (1) {
    10. sleep(1); // fake some uninteruptable work unit
    11.  
    12. QMutexLocker lock(&mutex);
    13. if (stopRequested)
    14. break;
    15. }
    16. emit finished();
    17. }
    18.  
    19. public:
    20. void requestStop() {
    21. QMutexLocker lock(&mutex);
    22. stopRequested = true;
    23. }
    24.  
    25. signals:
    26. void finished();
    27. private:
    28. QMutex mutex;
    29. bool stopRequested;
    30. };
    To copy to clipboard, switch view to plain text mode 
    requestStop() can be called to schedule an orderly shut down of the thread.

  9. #8
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    OK Thanks.
    I have decided that i whould like to wait until may thread has finished instead of exiting the thread when the user presses Cancel. How can i implement that?

    Qt Code:
    1. //Slot connected to finished signal of the worker class
    2. void a::threadFinished() {
    3. m_finished = true;
    4. }
    5.  
    6. void a::cancelPressed() {
    7. //Here I would like to wait until the thread is finished.
    8. bool stopWaiting = false;
    9. while(!stopWaiting) {
    10. m_mutex.lock();
    11. if (m_finished)
    12. stopWaiting = true;
    13. m_mutex.unlock();
    14. //How shall I implement the delay (if stopWaiting is false) before i continue another loop in the whileloop without locking the entire program?
    15. }
    16.  
    17. //Do som stuff after the thread has finished.
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QThread: Move owner of thread to thread with moveToThread

    Both the worker and the QThread issue a finished() signal when they are done. Connect to that signal and put your post-processing in the slot. Then you don't have to block the Qt event loop and GUI update.

  11. #10
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    But I whould like to exit the entire program when the user presses Cancel. I would like to make sure the threads are finished before I exit the program.

  12. #11
    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: QThread: Move owner of thread to thread with moveToThread

    Quote Originally Posted by SepticInsect View Post
    But I whould like to exit the entire program when the user presses Cancel. I would like to make sure the threads are finished before I exit the program.
    So when all threads are finished(), check if cancel was pressed, and if so, quit the application.
    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.


  13. The following user says thank you to wysota for this useful post:

    SepticInsect (12th September 2012)

  14. #12
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread: Move owner of thread to thread with moveToThread

    Thanks everyone for the help! The problem is solved!

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 03:27
  2. Replies: 1
    Last Post: 4th September 2012, 14:13
  3. multithread thread don't move
    By lzpmail in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2011, 09:09
  4. Replies: 5
    Last Post: 22nd February 2011, 21:21
  5. Finishing a thread in QThread
    By Tio in forum Newbie
    Replies: 9
    Last Post: 2nd June 2010, 17:38

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.