Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::stop_token

      From cppreference.com
      <cpp‎ |thread
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      stop_token
      (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_token;
      (since C++20)

      Thestop_token class provides the means to check if a stop request has been made or can be made, for its associatedstd::stop_source object. It is essentially a thread-safe "view" of the associated stop-state.

      Thestop_token can also be passed to the constructor ofstd::stop_callback, such that the callback will be invoked if thestop_token's associatedstd::stop_source is requested to stop. Andstop_token can be passed to the interruptible waiting functions ofstd::condition_variable_any, to interrupt the condition variable's wait if stop is requested.

      Contents

      [edit]Member alias templates

      Type Definition
      callback_type<Callback>(since C++26)std::stop_callback<Callback>

      [edit]Member functions

      constructs newstop_token object
      (public member function)[edit]
      destructs thestop_token object
      (public member function)[edit]
      assigns thestop_token object
      (public member function)[edit]
      Modifiers
      swaps twostop_token objects
      (public member function)[edit]
      Observers
      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_token objects
      (function)[edit]
      specializes thestd::swap algorithm
      (function)[edit]

      [edit]Notes

      Astop_token object is not generally constructed independently, but rather retrieved from astd::jthread orstd::stop_source. This makes it share the same associated stop-state as thestd::jthread orstd::stop_source.

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

      [edit]Example

      Run this code
      #include <iostream>#include <thread> usingnamespace std::literals::chrono_literals; void f(std::stop_token stop_token,int value){while(!stop_token.stop_requested()){std::cout<< value++<<' '<<std::flush;std::this_thread::sleep_for(200ms);}std::cout<<std::endl;} int main(){std::jthread thread(f,5);// prints 5 6 7 8... for approximately 3 secondsstd::this_thread::sleep_for(3s);// The destructor of jthread calls request_stop() and join().}

      Possible output:

      5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/stop_token&oldid=173981"

      [8]ページ先頭

      ©2009-2025 Movatter.jp