Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_future<T>::valid

      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
       
       
      bool valid()constnoexcept;
      (since C++11)

      Checks if the future refers to a shared state.

      This is the case only for futures that were not default-constructed or moved from. Unlikestd::future,std::shared_future's shared state is not invalidated whenget() is called.

      The behavior is undefined if any member function other than the destructor, the copy-assignment operator, the move-assignment operator, orvalid is called on ashared_future that does not refer to shared state (although implementations are encouraged to throwstd::future_error indicatingno_state in this case). It is valid to move or copy from a shared_future object for whichvalid() isfalse.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if*this refers to a shared state, otherwisefalse.

      [edit]Example

      Run this code
      #include <future>#include <iostream> int main(){std::promise<void> p;std::shared_future<void> f= p.get_future(); std::cout<<std::boolalpha; std::cout<< f.valid()<<'\n';    p.set_value();std::cout<< f.valid()<<'\n';    f.get();std::cout<< f.valid()<<'\n';}

      Output:

      truetruetrue

      [edit]See also

      waits for the result to become available
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/shared_future/valid&oldid=132965"

      [8]ページ先頭

      ©2009-2025 Movatter.jp