Results 1 to 16 of 16

Thread: Shutting down threads prematurely

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: Shutting down threads prematurely

    okai back to basics:

    Qt Code:
    1. for (int id=1;id<=guidata.numbers_of_workerthreads;id++)
    2. {
    3.  
    4. QThread *thread = new QThread;
    5.  
    6. Worker *worker = new Worker(&guidata,ui,id,&m_c,&mutex_file,&mutex_count);
    7.  
    8. worker->moveToThread(thread);
    9.  
    10. //connect events from worker<-> threads
    11. connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
    12. connect(thread, SIGNAL(started()), worker, SLOT(comets_to_use())); // when i start the thread it automaticly runs my slot that i specify
    13. connect(this, SIGNAL(shut_down_threads()), worker ,SLOT(on_shut_down_threads()));
    14. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    15. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    16. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    17.  
    18. thread->start();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    this is how i start a specific method in the worker other methods like my calc_stream_allcomets() ... would get exchanged with comets_to_use(); Depending on what button is clicked on the gui the worker slot that is executed on start is varied.

    As i said the calculations can be up to 1-2 days and sometimes i want to shut down the threads earlier e.g when you notice you made an input mistake of somekind.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Shutting down threads prematurely

    Ok, so depending on what you want to do you have a different slot of the worker connected to the thread's started() signal, but in all cases one worker object is only ever used for one operation and either cancelled or fully executed and then discarded.

    Ok, then comets_to_use() and any of the other operations that need to be interruptible need check whether they need to stop.
    And the slot connected to shut_down_threads() needs to be connected with a DirectConnection and needs to change whatever the worker does to check for stopping.

    One option would be to check the worker's thread isInterruptionRequested() and use the worker's thread requestInterrruption() to switch to stopping.
    The worker would then also make the thread quit its event loop right before it exits the processing function.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: Shutting down threads prematurely

    That sounds like a good approach to me. I'm still unclear how i would stop the worker from doing what it is doing to check if an interruption is wanted.

    What do I put in the worker slot that is connected? How would I change the worker from "working on my stuff". This is the Worker slot that is connected by direct connection to the main thread.

    Qt Code:
    1. void Worker::on_shut_down_threads()
    2. {
    3. if("workerthread_pointer?"->isInterruptionRequested())
    4. {
    5. ??
    6. }
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Am i Understanding u right? I think a little pseudo code or code would help me understand

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Shutting down threads prematurely

    No, in that slot you request the interruption.
    Qt Code:
    1. void Worker::on_shut_down_threads()
    2. {
    3. thread()->requestedInterruption();
    4. }
    To copy to clipboard, switch view to plain text mode 

    The method that does the long calculation needs regularily to check for interruption.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2015
    Posts
    33
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    14

    Default Re: Shutting down threads prematurely

    Okai anda,

    i think i've nearly understood, thx for your patience!

    In my worker slot i now have a check that is run through every 2sek. It checks if there is an interruption request from the main thread. The interrupt request is passed through with a direct connection. This is working at the moment. so in this part of the code how do i shut down the thread?


    this is in the executing worker slot (comets_to_use()) in the worker thread:

    Qt Code:
    1. //...worker doing stuff...
    2.  
    3. //comes past here every 2 sek
    4.  
    5. if (interruptRequested)
    6. {
    7. how do i shut down the thread from here?
    8. }
    To copy to clipboard, switch view to plain text mode 

    the only way i got it to work was to put this request at the beginning of all my nested loops and break; out of them so i quickly reach the end of the slot. then the thread processes my finished() signal and destructs normaly. is there a nicer way to do this?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Shutting down threads prematurely

    You can nest the calls

    Qt Code:
    1. void Worker::comets_to_use()
    2. {
    3. comets_to_use_internal();
    4. thread()->quit();
    5. }
    To copy to clipboard, switch view to plain text mode 
    That way you can just return from comets_in_use_internal() whenever you want to end.

    Cheers,
    _

Similar Threads

  1. Qt Threads vs Native Threads performance
    By StackOverflow in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2010, 12:14
  2. whatś the better in Threads
    By Tio in forum Newbie
    Replies: 15
    Last Post: 27th May 2010, 13:14
  3. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  4. Qt threads
    By ^Nisok^ in forum Newbie
    Replies: 12
    Last Post: 22nd April 2009, 14:35
  5. shutting down as user
    By safknw in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2006, 09:28

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
  •  
Qt is a trademark of The Qt Company.