Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::stop_token::stop_possible

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

      Checks if thestop_token object has associated stop-state, and that state either has already had a stop requested or it has associatedstd::stop_source object(s).

      A default constructedstop_token has no associated stop-state, and thus cannot be stopped; the associated stop-state for which nostd::stop_source object(s) exist can also not be stopped if such a request has not already been made.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      false if thestop_token object has no associated stop-state, or it did not yet receive a stop request and there are no associatedstd::stop_source object(s);true otherwise.

      [edit]Notes

      If thestop_token object has associated stop-state and a stop request has already been made, this function still returnstrue.

      If thestop_token object has associated stop-state from astd::jthread—for example, thestop_token was retrieved by invokingget_stop_token() on astd::jthread object—then this function always returnstrue. Astd::jthread always has an internalstd::stop_source object, even if the thread's invoking function does not check it.

      [edit]Example

      Run this code
      #include <chrono>#include <condition_variable>#include <format>#include <iostream>#include <mutex>#include <string_view>#include <thread>usingnamespace std::chrono_literals; int main(){std::cout<<std::boolalpha;auto print=[](std::string_view name,conststd::stop_token& token){std::cout<<std::format("{}: stop_possible = {:s}, stop_requested = {:s}\n",             name, token.stop_possible(), token.stop_requested());}; // A worker thread that will listen to stop requestsauto stop_worker=std::jthread([](std::stop_token stoken){for(int i=10; i;--i){std::this_thread::sleep_for(300ms);if(stoken.stop_requested()){std::cout<<"  Sleepy worker is requested to stop\n";return;}std::cout<<"  Sleepy worker goes back to sleep\n";}}); // A worker thread that will only stop when completedauto inf_worker=std::jthread([](){for(int i=5; i;--i){std::this_thread::sleep_for(300ms);std::cout<<"  Run as long as we want\n";}}); std::stop_token def_token;std::stop_token stop_token= stop_worker.get_stop_token();std::stop_token inf_token= inf_worker.get_stop_token();    print("def_token ", def_token);    print("stop_token", stop_token);    print("inf_token ", inf_token); std::cout<<"\nRequest and join stop_worker:\n";    stop_worker.request_stop();    stop_worker.join(); std::cout<<"\nRequest and join inf_worker:\n";    inf_worker.request_stop();    inf_worker.join();std::cout<<'\n';     print("def_token ", def_token);    print("stop_token", stop_token);    print("inf_token ", inf_token);}

      Possible output:

      def_token : stop_possible = false, stop_requested = falsestop_token: stop_possible = true, stop_requested = falseinf_token : stop_possible = true, stop_requested = false Request and join stop_worker:  Run as long as we want  Sleepy worker is requested to stop Request and join inf_worker:  Run as long as we want  Run as long as we want  Run as long as we want  Run as long as we want def_token : stop_possible = false, stop_requested = falsestop_token: stop_possible = true, stop_requested = trueinf_token : stop_possible = true, stop_requested = true
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/stop_token/stop_possible&oldid=173038"

      [8]ページ先頭

      ©2009-2026 Movatter.jp