Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::future<T>::get

      From cppreference.com
      <cpp‎ |thread‎ |future
       
       
      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
       
       
      Main template
      T get();
      (1)(since C++11)
      std::future<T&> specializations
      T& get();
      (2)(since C++11)
      std::future<void> specialization
      void get();
      (3)(since C++11)

      Theget member function waits (by callingwait()) until the shared state is ready, then retrieves the value stored in the shared state (if any). Right after calling this function,valid() isfalse.

      Ifvalid() isfalse before the call to this function, the behavior is undefined.

      Contents

      [edit]Return value

      1) The valuev stored in the shared state, asstd::move(v).
      2) The reference stored as value in the shared state.
      3) (none)

      [edit]Exceptions

      If an exception was stored in the shared state referenced by the future (e.g. via a call tostd::promise::set_exception()) then that exception will be thrown.

      [edit]Notes

      The C++ standard recommends the implementations to detect the case whenvalid() isfalse 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 <string>#include <thread> std::string time(){staticauto start=std::chrono::steady_clock::now();std::chrono::duration<double> d=std::chrono::steady_clock::now()- start;return"["+std::to_string(d.count())+"s]";} int main(){usingnamespace std::chrono_literals; {std::cout<< time()<<" launching thread\n";std::future<int> f=std::async(std::launch::async,[]{std::this_thread::sleep_for(1s);return7;});std::cout<< time()<<" waiting for the future, f.valid() = "<< f.valid()<<'\n';int n= f.get();std::cout<< time()<<" f.get() returned "<< n<<", f.valid() = "<< f.valid()<<'\n';} {std::cout<< time()<<" launching thread\n";std::future<int> f=std::async(std::launch::async,[]{std::this_thread::sleep_for(1s);returntrue?throwstd::runtime_error("7"):7;});std::cout<< time()<<" waiting for the future, f.valid() = "<< f.valid()<<'\n'; try{int n= f.get();std::cout<< time()<<" f.get() returned "<< n<<", f.valid() = "<< f.valid()<<'\n';}catch(conststd::exception& e){std::cout<< time()<<" caught exception "<< e.what()<<", f.valid() = "<< f.valid()<<'\n';}}}

      Possible output:

      [0.000004s] launching thread[0.000461s] waiting for the future, f.valid() = 1[1.001156s] f.get() returned with 7, f.valid() = 0[1.001192s] launching thread[1.001275s] waiting for the future, f.valid() = 1[2.002356s] caught exception 7, f.valid() = 0

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2096C++11overload(1) needed to check whetherT isMoveAssignablenot required

      [edit]See also

      checks if the future has a shared state
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/future/get&oldid=169902"

      [8]ページ先頭

      ©2009-2025 Movatter.jp