Results 1 to 7 of 7

Thread: how to resume QThread::wait()?

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default how to resume QThread::wait()?

    Hi,
    I am trying to run a class's void function called "show()", here is code:

    in main.cpp
    Qt Code:
    1. MyClassObj* cObj;
    2. QThread thread;
    3.  
    4.  
    5. for (int i=0 ; i<=vecObj.count() ; i++)
    6. {
    7. cObj=&vecObj[i][0];
    8. cObj->moveToThread(&thread);
    9.  
    10.  
    11. cObj->connect(&thread,SIGNAL(started()),SLOT(show()));
    12. thread.connect(&thread,SIGNAL(finished()),&thread,SLOT(quit()));
    13.  
    14.  
    15. thread.start();
    16. thread.wait();
    17.  
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    now issue is, this code runs fine for the first loop, but since thread.wait() is called at the end of loop, thread waits for execution to stop (waiting for thread.quit()) after first loop and i am not able "resume" this wait() condition so that show() is called again for next loop.

    I am kinda stuck here, i have been going through many articles and forum discussion, haven't found any issue like this.

    Any help will be greatfully appreciated.
    Thanks in advance.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to resume QThread::wait()?

    If you want your object to work sequentially, (you wait for each obj to finish before you start the next) then what do you need a thread for?
    Lets say, its because the work done on each object is long, and you want the GUI responsive - ok.
    On way is to get rid of the wait, and have a "thread starter" slot connected to the finished() signal of the threads, and then you can start the next.

    But it looks to me you are trying to force using threads where they are not really needed, and that a proper design would simplify the whole thing dramatically.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: how to resume QThread::wait()?

    thanks for reply

    Quote Originally Posted by high_flyer View Post
    But it looks to me you are trying to force using threads where they are not really needed, and that a proper design would simplify the whole thing dramatically.
    Yes you are right, i am trying to force using thread but it is needed at the same time, because of the same reason you mentioned, to keep my GUI responsive, without having threads my GUI will just go unresponsive.

    Quote Originally Posted by high_flyer View Post
    On way is to get rid of the wait, and have a "thread starter" slot connected to the finished() signal of the threads, and then you can start the next.
    i think you meant by:

    Qt Code:
    1. cObj->connect(&thread,SIGNAL(started()),SLOT(show()));
    2. thread.connect(&thread,SIGNAL(finished()),&thread,SLOT(start()));
    3.  
    4.  
    5. thread.start();
    6. //thread.wait(); //COMMENTED THIS OUT
    To copy to clipboard, switch view to plain text mode 

    but obviously it wont work either because none of thread is waiting to finish...and if i uncomment above thread.wait() then execution of thread never finishes to start next one, i mean finished() signal is never emitted from &thread obj.
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to resume QThread::wait()?

    There are all kinds of problems in what you are saying...
    But I'll start from the end:
    If you have some work that needs to be done out side the GUI thread, and that work has to be sequential, then why do you put all the stages in different threads?
    Have one thread call the stages one aftter the other, instead of putting each stage in its own thread.
    So something like:
    1. Have your thread object take a list of the objects.
    2. In run() iterate over the list and call the work functions on the objects.
    3. In the gui thread just have your one worker thread notify you when it is finished.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: how to resume QThread::wait()?

    Quote Originally Posted by high_flyer View Post
    Have one thread call the stages one aftter the other, instead of putting each stage in its own thread.
    thanks so much
    while i was waiting for a reply to my thread, this was exact same approach i tried, but i came across a problem,

    now instead of having different threads in one loop, i have loop in one thread. Below is code for it followed by description of issue i am facing:

    Qt Code:
    1. ItemList::ItemList()
    2. {
    3.  
    4. cObj *_cobj;
    5. _cobj=new cObj("arguments passed to constructor");
    6. _cobj.properties="set properties and stuffs";
    7.  
    8. list.append(_cobj); //QVector object of 'cObj' type
    9. _cobj=new cObj("diffr");
    10. _cobj.properties="properties for this instance";
    11.  
    12. list.append(_cobj); //QVector object of 'cObj' type
    13. createThread(); // to create thread...definition is below
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ListObj::createThread()
    2. {
    3. QThread *thread=new QThread;
    4. cObj _cObj1=_cObjGLOBAL;
    5.  
    6. _cObj1->moveToThread(thread);
    7.  
    8.  
    9.  
    10. cObj1->connect(thread,SIGNAL(started()),SLOT(startProcess())); //<<--------------------- This line
    11. thread.connect(&thread,SIGNAL(finished()),&thread,SLOT(quit()));
    12.  
    13.  
    14. thread.start();
    15. thread.wait();
    16. }
    17.  
    18. void ListObj::startProcess() //<<------------------------ REFERENCE1
    19. {
    20. for (i=0;i< list.count(); i++)
    21. {
    22. //process for each iteration
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 


    NOW problem is where above code lies "This line"... actually connect() looks for startProcess() in cObj, but i want reference to "REFERENCE1", i have tried following:

    Qt Code:
    1. cObj1->connect(thread,SIGNAL(started()),this,SLOT(startProcess())); //<<----------with "this" as *receiver
    To copy to clipboard, switch view to plain text mode 

    but it gave me compile error:
    no matching function for call to ‘_cObj::connect(QThread*&, const char*, ListObj* const, const char*)’

    Quote Originally Posted by naturalpsychic View Post
    but it gave me compile error:
    no matching function for call to ‘_cObj::connect(QThread*&, const char*, ListObj* const, const char*)’

    Oh oki i have overcome this problem by looking at original signature of connect()...since my ListObj doesn't inherit QObject so it is not considered as QObject, what i did i inherited QObject and now everything is working all good...


    Thanks alot guys
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to resume QThread::wait()?

    For this kind of execution you might want to have a look at QtConcurrent.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    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: how to resume QThread::wait()?

    I suggest you read [wiki]Keeping the GUI Responsive[/wiki]. And also read what QThread::wait() is for and why it doesn't work in your case. Also read about QWaitCondition. But focus on the first link I gave you, it's a comprehensive article describing different approaches you may take.
    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. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 15:24
  2. Resume download with QHttp
    By THRESHE in forum Qt Programming
    Replies: 7
    Last Post: 17th April 2010, 10:12
  3. QThread blocking wait()
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2009, 09:21
  4. QFTP resume download
    By wagui in forum Qt Programming
    Replies: 9
    Last Post: 14th March 2008, 14:15

Tags for this Thread

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.