The future, promise and parked of C + + 11_ Task Usage


Introduction to future

A future is an object that can retrieve a value from some provider object or function, properly synchronizing this access if in different threads.

It can get results from asynchronous object or function tasks. It is usually the same as STD:: async, promise, packaged_ Tasks call each other.

The future object can be used when the valid is valid. The default constructor is valid and false. Therefore, when using the future object, the object needs to be initialized.

Member functions of future

Member function

Future usage

Future can get related results or thread output results from multithreading. Because the output of the thread will not be output immediately (waiting for the execution to complete), future will have a shared state to judge the current future state.

Future output results will have three states, the type is STD:: future_ Status, they are

  • Deferred: not yet;
  • Ready: completed;
  • Timeout: execution times out

There are several ways for future to get results: get, wait, wait_ for。

1. Get waits for the end of the asynchronous operation and returns the result;

2. Wait is just waiting for the asynchronous operation to complete;

3、wait_ For is a timeout waiting for the return result.

 Examples of future and async

#include <iostream>
#include <future>
#include <unistd.h>

int main(int argc, char **argv) {

  std::future_status f_status;

  auto func = [](int b) -> int{
      std::cout << "async thread running...." << std::endl;
      for (int i = 0; i< b; i++) {
      return b;
  std::future<int> fut = std::async(std::launch::async, func, 888888888);
  std::cout << "main thread running ...." << std::endl;
  std::chrono::milliseconds span(100);
  //Method 1 is used to wait for data of asynchronous operation
  while(1) {
    f_status = fut.wait_for(span);
    if (f_status == std::future_status::ready) {
      std::cout << "future_status::ready   ";
    } else if(f_status == std::future_status::deferred) {
      std::cout << "future_status::deferred ";
    } else if(f_status == std::future_status::timeout) {
      std::cout << "future_status::timeout  ";
    std::cout << std::endl;
    std::cout << "................";
    std::cout << std::endl;
    // fut.wait (); // method 2: wait for the data of the asynchronous thread
  std::cout << "future result " << fut.get() << std::endl;
  return 0;

Compile script

cmake_minimum_required(VERSION 2.6)
set(src ./async.cpp)
set(exe "async")
set(CMAKE_CXX_FLAGS -std=c++11)
add_executable(${exe} ${src})
target_link_libraries(${exe} -lpthread)


STD:: promise provides convenience for obtaining a value in the thread function. In the thread function, assign a value to the promise passed in from outside. After the thread function is executed, the value can be obtained through promise. It is worth noting that the value is obtained indirectly through the future provided by promise.

Promise member function

Member function

Its basic usage:

 std::promise<int> pr;
  std::thread t([](std::promise<int>& p){ p.set_value_at_thread_exit(9); },std::ref(pr));
  std::future<int> f = pr.get_future();
  auto r = f.get();



std::packaged_ Task it encapsulates a callable target (such as function, lambda expression, bind expression, or other function object) for asynchronous call. It is similar to promise in some degree. Promise saves a shared state value, while packaged_ Task holds a function. Its basic usage:

 std::packaged_task<int()> task([](){ return 7; });
  std::thread t1(std::ref(task)); 
  std::future<int> f1 = task.get_future(); 
  auto r1 = f1.get();

Promise and packaged_ The relationship between task and STD:: future

STD:: future provides a mechanism to access the result of asynchronous operation. It is a low-level object with thread, and STD:: packaged is a higher-level object_ Task and STD:: promise, both of which have future in order to access asynchronous operation results, STD:: packaged_ Task encapsulates an asynchronous operation, while STD:: promise encapsulates a value, which is for the convenience of asynchronous operation. Sometimes I need to get a value in the thread, so I use STD:: promise. Sometimes I need to get a return value of an asynchronous operation, so I use STD:: packaged_ Task, that is to say, the white dot is promise, which gets the value set in the execution process, while packaged_ Task gets the result.

Reference section:

This article is about the future, promise and parked of C + + 11_ This is the article about task. More about C + + 11 future promise parked_ Task content please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!