Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::stop_source

      From cppreference.com
      <cpp‎ |thread
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      (C++20)
      stop_source
      (C++20)
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Condition variables
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      (C++20)
      Futures
      (C++11)
      (C++11)
      (C++11)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
       
      Defined in header<stop_token>
      class stop_source;
      (since C++20)

      Thestop_source class provides the means to issue a stop request, such as forstd::jthread cancellation. A stop request made for onestop_source object is visible to allstop_sources andstd::stop_tokens of the same associated stop-state; anystd::stop_callback(s) registered for associatedstd::stop_token(s) will be invoked, and anystd::condition_variable_any objects waiting on associatedstd::stop_token(s) will be awoken.

      Once a stop is requested, it cannot be withdrawn. Additional stop requests have no effect.

      Contents

      [edit]Member functions

      constructs newstop_source object
      (public member function)[edit]
      destructs thestop_source object
      (public member function)[edit]
      assigns thestop_source object
      (public member function)[edit]
      Modifiers
      makes a stop request for the associated stop-state, if any
      (public member function)[edit]
      swaps twostop_source objects
      (public member function)[edit]
      Observers
      returns astop_token for the associated stop-state
      (public member function)[edit]
      checks whether the associated stop-state has been requested to stop
      (public member function)[edit]
      checks whether associated stop-state can be requested to stop
      (public member function)[edit]

      [edit]Non-member functions

      (C++20)
      compares twostd::stop_source objects
      (function)[edit]
      specializes thestd::swap algorithm
      (function)[edit]

      [edit]Helper tags

      a tag used forstop_source to indicate no associated stop-state upon construction
      (tag)[edit]

      [edit]Notes

      For the purposes ofstd::jthread cancellation thestop_source object should be retrieved from thestd::jthread object usingget_stop_source(); or stop should be requested directly from thestd::jthread object usingrequest_stop(). This will then use the same associated stop-state as that passed into thestd::jthread's invoked function argument (i.e., the function being executed on its thread).

      For other uses, however, astop_source can be constructed separately using the default constructor, which creates new stop-state.

      Feature-test macroValueStdFeature
      __cpp_lib_jthread201911L(C++20)Stop token andjoining thread

      [edit]Example

      Run this code
      #include <chrono>#include <iostream>#include <stop_token>#include <thread> usingnamespace std::chrono_literals; void worker_fun(int id,std::stop_token stoken){for(int i=10; i;--i){std::this_thread::sleep_for(300ms);if(stoken.stop_requested()){std::printf("  worker%d is requested to stop\n", id);return;}std::printf("  worker%d goes back to sleep\n", id);}} int main(){std::jthread threads[4];std::cout<<std::boolalpha;auto print=[](const std::stop_source& source){std::printf("stop_source stop_possible = %s, stop_requested = %s\n",                    source.stop_possible()?"true":"false",                    source.stop_requested()?"true":"false");}; // Common source    std::stop_source stop_source;     print(stop_source); // Create worker threadsfor(int i=0; i<4;++i)        threads[i]=std::jthread(worker_fun, i+1, stop_source.get_token()); std::this_thread::sleep_for(500ms); std::puts("Request stop");    stop_source.request_stop();     print(stop_source); // Note: destructor of jthreads will call join so no need for explicit calls}

      Possible output:

      stop_source stop_possible = true, stop_requested = false  worker2 goes back to sleep  worker3 goes back to sleep  worker1 goes back to sleep  worker4 goes back to sleepRequest stopstop_source stop_possible = true, stop_requested = true  worker3 is requested to stop  worker1 is requested to stop  worker2 is requested to stop  worker4 is requested to stop
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/stop_source&oldid=173680"

      [8]ページ先頭

      ©2009-2025 Movatter.jp