[ Web Proxy ]
URL:
Viewing: https://ko.cppreference.com/cpp/thread/future [Back]  [Original]

std::future - cppreference.com
cppreference.com

std::future

cppreference.com
<tbody> </tbody>
<future> .
template< class T > class future;
(1) (since C++11)
template< class T > class future<T&>;
(2) (since C++11)
template<> class future<void>;
(3) (since C++11)
std::future         .:
  • (std::async, std::packaged_task, std::promise ) std::future .
  • std::future , , . .

std::future (std::shared_future .)

constructs the future object
(public member function) [edit]
destructs the future object
(public member function) [edit]
moves the future object
(public member function) [edit]
transfers the shared state from *this to a shared_future and returns it
(public member function) [edit]
Getting the result
returns the result
(public member function) [edit]
State
checks if the future has a shared state
(public member function) [edit]
waits for the result to become available
(public member function) [edit]
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]

#include <iostream>
#include <future>
#include <thread>

int main()
{
    // packaged_task  future 
    std::packaged_task<int()> task([](){ return 7; }); //  
    std::future<int> f1 = task.get_future();  // future 
    std::thread(std::move(task)).detach(); //  

    // an async() future 
    std::future<int> f2 = std::async(std::launch::async, [](){ return 8; });

    // promise future 
    std::promise<int> p;
    std::future<int> f3 = p.get_future();
    std::thread( [](std::promise<int> p){ p.set_value_at_thread_exit(9); }, 
                 std::move(p) ).detach();

    std::cout << "Waiting..." << std::flush;
    f1.wait();
    f2.wait();
    f3.wait();
    std::cout << "Done!\nResults are: "
              << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
}

Output:

Waiting...Done!
Results are: 7 8 9

See also

(C++11)
runs a function asynchronously (potentially in a new thread) and returns a std::future that will hold the result
(function template) [edit]
waits for a value (possibly referenced by other futures) that is set asynchronously
(class template) [edit]

Web Proxy Viewer  |  New URL  |  Original Page