WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Advanced · question 59 of 100

Discuss the use of ’std::function’ and ’std::future’ in C++ for asynchronous programming.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Asynchronous programming allows a program to execute tasks concurrently, enabling the efficient use of system resources. C++ provides several features to support asynchronous programming, including std::function and std::future.

std::function is a type-erased function wrapper that can store and invoke any callable object, including function pointers, lambda functions, and functors. It can be used to create a callback mechanism that allows one part of a program to register a function to be called later by another part of the program.

For example, suppose we have a function void perform_async_task(std::function<void()> task) that takes a std::function object representing a task to be performed asynchronously. We can call this function and pass a lambda function that prints a message as follows:

    perform_async_task([](){
        std::cout << "Performing task asynchronouslyn";
    });

std::future is a synchronization primitive that represents a value that may not be available yet. It provides a way for a thread to wait for a value to be computed asynchronously by another thread.

For example, suppose we have a function int compute_async_value() that performs a long-running computation and returns a result asynchronously. We can call this function using std::async and obtain a std::future object that represents the result as follows:

    std::future<int> future_result = std::async(std::launch::async, [](){
        // long-running computation
        return 42;
    });

We can then wait for the result using the std::future::get() method, which blocks until the result is available:

    int result = future_result.get();

std::future can also be used to implement continuations, which are functions that are executed when the asynchronous operation completes. For example, we can use std::future::then() to execute a lambda function when the result of a computation becomes available:

    std::future<int> future_result = std::async(std::launch::async, [](){
        // long-running computation
        return 42;
    });
    
    std::future<void> continuation = future_result.then([](int result){
        std::cout << "Result is " << result << std::endl;
    });
    

In this example, the lambda function passed to std::future::then() takes the result of the computation as its argument and prints it to the console. The std::future<void> object returned by std::future::then() represents the continuation and can be used to wait for its completion or to chain further continuations.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics