Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::jthread::request_stop

      From cppreference.com
      <cpp‎ |thread‎ |jthread
       
       
      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 request_stop()noexcept;
      (since C++20)

      Issues a stop request to the internal stop-state, if it has not yet already had stop requested.

      The determination is made atomically, and if stop was requested, the stop-state is atomically updated to avoid race conditions, such that:

      • stop_requested() andstop_possible() can be concurrently invoked on otherstd::stop_tokens andstd::stop_sources of the same shared stop-state.
      • request_stop() can be concurrently invoked from multiple threads on the samejthread object or on otherstd::stop_source objects associated with the same stop-state, and only one will actually perform the stop request.

      However, see the Notes section.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if this invocation made a stop request, otherwisefalse.

      [edit]Postconditions

      For astd::stop_token retrieved byget_stop_token() or astd::stop_source retrieved byget_stop_source(),stop_requested() istrue.

      [edit]Notes

      If therequest_stop() does issue a stop request (i.e., returnstrue), then anystd::stop_callbacks registered for the same associated stop-state will be invoked synchronously, on the same threadrequest_stop() is issued on. If an invocation of a callback exits via an exception,std::terminate is called.

      If a stop request has already been made, this function returnsfalse. However there is no guarantee that another thread orstd::stop_source object which has just (successfully) requested stop for the same stop-state is not still in the middle of invoking astd::stop_callback function.

      If therequest_stop() does issue a stop request (i.e., returnstrue), then all condition variables of base typestd::condition_variable_any registered with an interruptible wait forstd::stop_tokens associated with thejthread's internal stop-state will be awoken.

      [edit]Example

      Run this code
      #include <chrono>#include <condition_variable>#include <iostream>#include <mutex>#include <thread> usingnamespace std::chrono_literals; // Helper function to quickly show which thread printed whatvoid print(auto txt){std::cout<<std::this_thread::get_id()<<' '<< txt;} int main(){// A sleepy worker threadstd::jthread sleepy_worker([](std::stop_token stoken){for(int i=10; i;--i){std::this_thread::sleep_for(300ms);if(stoken.stop_requested()){                    print("Sleepy worker is requested to stop\n");return;}                print("Sleepy worker goes back to sleep\n");}}); // A waiting worker thread// The condition variable will be awoken by the stop request.std::jthread waiting_worker([](std::stop_token stoken){std::mutex mutex;std::unique_lock lock(mutex);std::condition_variable_any().wait(lock, stoken,[]{returnfalse;});            print("Waiting worker is requested to stop\n");return;}); // Sleep this thread to give threads time to spinstd::this_thread::sleep_for(400ms); // std::jthread::request_stop() can be called explicitly:    print("Requesting stop of sleepy worker\n");    sleepy_worker.request_stop();    sleepy_worker.join();    print("Sleepy worker joined\n"); // Or automatically using RAII:// waiting_worker's destructor will call request_stop()// and join the thread automatically.}

      Possible output:

      140287602706176 Sleepy worker goes back to sleep140287623300928 Requesting stop of sleepy worker140287602706176 Sleepy worker is requested to stop140287623300928 Sleepy worker joined140287594313472 Waiting worker is requested to stop
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/jthread/request_stop&oldid=176995"

      [8]ページ先頭

      ©2009-2025 Movatter.jp