stop_callback

Stop callbacks are callback functions that are attached to thread stop_requests. Once a stop request is issued, the callback function is executed.

Note that the callback is executed on the thread that registered it, not on the thread that was stopped by the stop request.

#include <iostream>
#include <syncstream>

#include <thread>

void task
   (
   ::std::stop_token
      stop_token
   )
{
   while
      (
         not stop_token.stop_requested()
      )
   {
      ::std::this_thread::sleep_for
         (
         ::std::chrono::seconds(1)
         )
         ;
   }
   
   ::std::osyncstream(::std::cout) << "Thread "
                                   << ::std::
                                         this_thread::
                                            get_id()
                                   << " stopped"
                                   << ::std::endl
                                      ;
}

int main(int argc, char ** argv)
{
   ::std::jthread
      thread( task );
   
   ::std::this_thread::sleep_for
      (
      ::std::chrono::seconds(3)
      )
      ;
   
   ::std::stop_callback
      callback
         (
         thread.get_stop_token(),
         [] (void)
            {
               ::std::osyncstream(::std::cout)
                  << "Thread "
                  << ::std::this_thread::get_id()
                  << " callback"
                  << ::std::endl
                     ;
            }
         )
         ;
   
   //
   // Stop the thread:
   //
   
   thread.request_stop();
   
   return 0;
}

Possible output:

Thread 139764672713664 callback Thread 139764665677376 stopped