std::future
时间: 2023-10-19 16:06:22 浏览: 44
std::future is a class template in C++11 and later versions of the language that represents a value or an exception that may not yet be available. It is used for asynchronous programming and allows you to execute a task in a separate thread while the main thread continues with other tasks.
A future object is created by a promise object, which is used to set the value or exception that the future will eventually hold. The future can then be used to retrieve the value or exception when it becomes available.
The future class template provides several member functions, such as get() and wait(), to retrieve the value or exception of the future. The get() function blocks until the value or exception is available, while the wait() function blocks until the future is ready but does not retrieve the value or exception.
In addition, the future class template provides the ability to chain futures together using the then() member function, which allows you to specify a callback function to be executed when the future becomes ready.
Overall, std::future is a powerful tool for writing asynchronous code in C++ and allows developers to take advantage of multi-threading and parallel processing to improve the performance of their programs.