Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_future<T>::wait_for

      From cppreference.com
      <cpp‎ |thread‎ |shared future

      [edit template]
       
       
      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
       
       
      template<class Rep,class Period>
      std::future_status wait_for(conststd::chrono::duration<Rep,Period>& timeout_duration)const;
      (since C++11)

      Waits for the result to become available. Blocks until specifiedtimeout_duration has elapsed or the result becomes available, whichever comes first. The return value identifies the state of the result.

      If the future is the result of a call tostd::async that used lazy evaluation, this function returns immediately without waiting.

      This function may block for longer thantimeout_duration due to scheduling or resource contention delays.

      The standard recommends that a steady clock is used to measure the duration. If an implementation uses a system clock instead, the wait time may also be sensitive to clock adjustments.

      The behavior is undefined ifvalid() isfalse before the call to this function.

      Contents

      [edit]Parameters

      timeout_duration - maximum duration to block for

      [edit]Return value

      Constant Explanation
      future_status::deferred The shared state contains a deferred function using lazy evaluation, so the result will be computed only when explicitly requested
      future_status::ready The result is ready
      future_status::timeout The timeout has expired

      [edit]Exceptions

      Any exception thrown by clock, time_point, or duration during the execution (clocks, time points, and durations provided by the standard library never throw).

      [edit]Notes

      The implementations are encouraged to detect the case whenvalid==false before the call and throw astd::future_error with an error condition ofstd::future_errc::no_state.

      [edit]Example

      Run this code
      #include <chrono>#include <future>#include <iostream>#include <thread>usingnamespace std::chrono_literals; int main(){std::shared_future<int> future=std::async(std::launch::async,[](){std::this_thread::sleep_for(3s);return8;}); std::cout<<"waiting...\n";std::future_status status; do{switch(status= future.wait_for(1s); status){casestd::future_status::deferred:std::cout<<"deferred\n";break;casestd::future_status::timeout:std::cout<<"timeout\n";break;casestd::future_status::ready:std::cout<<"ready!\n";break;}}while(status!=std::future_status::ready); std::cout<<"result is "<< future.get()<<'\n';}

      Possible output:

      waiting...timeouttimeouttimeoutready!result is 8

      [edit]See also

      waits for the result to become available
      (public member function)[edit]
      waits for the result, returns if it is not available until specified time point has been reached
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/shared_future/wait_for&oldid=132984"

      [8]ページ先頭

      ©2009-2025 Movatter.jp