Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_future<T>::wait

      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
       
       
      void wait()const;
      (since C++11)

      Blocks until the result becomes available.valid()==true after the call.

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

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      (none)

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [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.

      Calling wait on the samestd::shared_future from multiple threads is not safe; the intended use is for each thread that waits on the same shared state to have a copy of astd::shared_future.

      [edit]Example

      Run this code
      #include <chrono>#include <future>#include <iostream>#include <thread> int fib(int n){if(n<3)return1;elsereturn fib(n-1)+ fib(n-2);} int main(){std::shared_future<int> f1=std::async(std::launch::async,[](){return fib(40);});std::shared_future<int> f2=std::async(std::launch::async,[](){return fib(43);}); std::cout<<"waiting... "<<std::flush;constauto start=std::chrono::system_clock::now();     f1.wait();    f2.wait(); constauto diff=std::chrono::system_clock::now()- start;std::cout<<std::chrono::duration<double>(diff).count()<<" seconds\n"; std::cout<<"f1: "<< f1.get()<<'\n';std::cout<<"f2: "<< f2.get()<<'\n';}

      Possible output:

      waiting... 1.61803 secondsf1: 102334155f2: 433494437

      [edit]See also

      waits for the result, returns if it is not available for the specified timeout duration
      (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&oldid=132983"

      [8]ページ先頭

      ©2009-2025 Movatter.jp