GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C++ Library - <future>
Selected Reading
C++ Library - <future>The <future> header in C++ provides a way to handle asynchronous operations, allowing tasks to be run concurrently and results to be retrieved at a later point. this library is part of the broader concurrency, which enables multi-threaded programming with ease. It is typically used in combination with std::async, which runs a function asynchronously, or with the std::promise, which manually sets the value for a future object. when the value is ready, it can be accessed using the get() function of the future object. The commonly used C++ <future> copying functions are listed below along with their description. Including <future> HeaderTo include the <future> header in your C++ program, you can use the following syntax. #include <future> Functions of <future> HeaderBelow is list of all functions from <future> header.
Waiting for Future to be ReadyIn the following example, we are going to use the wait() to block the main thread until the asynchronous operation is complete. #include <iostream>
#include <future>
#include <thread>
int a(int x, int y) {
std::this_thread::sleep_for(std::chrono::seconds(2));
return x + y;
}
int main() {
std::future < int > b = std::async (std::launch::async, a, 2, 1);
b.wait();
std::cout << "Result : " << b.get() << std::endl;
return 0;
}
OutputFollowing is the output of the above code − Result : 3 Advertisements
| ||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |