
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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> Header
To include the <future> header in your C++ program, you can use the following syntax.
#include <future>
Functions of <future> Header
Below is list of all functions from <future> header.
Sr.No | Functions & Description |
---|---|
1 |
operator=
It assigns the shared state. |
2 |
get_future
It returns a future associated with associated with the result. |
3 |
set_value
It sets the result to specific value. |
4 |
set_exception
It sets the result to indicate an exception. |
5 |
set_value_at_thread_exit
It sets the result to specific value while delivering the notification only at thread exit. |
6 |
swap
It swaps two objects. |
7 |
valid
It check for valid shared state. |
8 |
operator()
It calls the stored task. |
9 |
wait
It waits for the result to become available. |
Waiting for Future to be Ready
In 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; }
Output
Following is the output of the above code −
Result : 3