Results 1 to 5 of 5

Thread: QThread - how to pause and resume

  1. #1
    Join Date
    Dec 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread - how to pause and resume

    Hello guys,

    how is possible pause and then resume thread in QT?
    My application starts http proxy server in new thread and in some moments I need pause the thread with proxy and get/set data from comunication process between client and server.
    I tried this,

    Qt Code:
    1. proxy::proxy(sqlitt * sq) {
    2.  
    3. pending = false;
    4. }
    5.  
    6. void proxy::run(){
    7.  
    8. smw = new sslMeatWrapper(this);
    9.  
    10. }
    11.  
    12. // this method is called from sslMeatWrapper when new request arrives
    13. void proxy::proxyRequest() {
    14. qDebug << "Incoming proxy request";
    15. pending = true;
    16. // in this moment I need to pause thread and get/set data from/to smw object
    17. while (pending)
    18. msleep(1000);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    In GUI class there is a slot

    Qt Code:
    1. void sqlitt::next() {
    2. pr->pending = true;
    3. }
    To copy to clipboard, switch view to plain text mode 

    but this is not working. The thread is stocked on while statement i suppose. Can you please advice?

    Thanks

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread - how to pause and resume

    If you modify a variable from a different thread it should be volatile, otherwise compiler may optimize access to it and while loop won't "notice" the change.
    Think of the thread safety, accessing the same resource from multiple threads requires protection.
    Are you sure you really need a thread in your case? Most things in qt can be done via signal/slot in asynchronous mode.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread - how to pause and resume

    Mental, how do you think asynch calls are made without threads

    Op, there isn't some easy function you can call to pause a thread. You should think about what you need to get the data safely - this does not include using non-thread-safe variables and ugly sleeps

    Do you really need to do it in 'this' order? Can you make a safer way to communicate between your classes?
    Last edited by amleto; 7th March 2012 at 08:16.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread - how to pause and resume

    Socket work asynchronously without threads, so maybe this is what he needs.

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

    Default Re: QThread - how to pause and resume

    how is possible pause and then resume thread in QT?
    My application starts http proxy server in new thread and in some moments I need pause the thread with proxy and get/set data from comunication process between client and server.
    You should read about general threaded programming concepts.
    Specifically about mutexes (QMutex) and wait conditions (QWaitCondition).
    ==========================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.

Similar Threads

  1. how to resume QThread::wait()?
    By naturalpsychic in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2011, 23:07
  2. QFTP resume download
    By wagui in forum Qt Programming
    Replies: 9
    Last Post: 14th March 2008, 14:15
  3. I need a pause (QTimer)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2008, 05:37
  4. Pause executing QProcess
    By bravetanveer in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2006, 07:55

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.