C++11 thread library and std::async

C++11 thread library and std::async

The C++ Thread Class Library was launched to support and enable multithreading programming concepts in the C++ programming language. One of the features of the std::async class library is the multitasking, multiple-thread class library development within a single program. The std::async Thread Class Library enables C++ users to perform asynchronous tasks, meaning they are thread tasks that run simultaneously with the rest of the program, without the need to manage the threads in the proper order.

C++11 thread library and stdasync

Overview of the Thread Library in the C++11 version of C++ Programming.

The Thread Library in the C++11 version provides several components for multithreading tasks within a single program, including several commonly used elements.

Elements of the Thread Library in the C++11 version.

  • std::thread – This thread class library is used to create and manage threads in a C++ program.
  • std::mutex – This thread class library is used to implement mutual exclusion (locking thread class element resources to avoid data races) in a C++ program.
  • std::lock_guard – This thread class library is used to secure mutex data in a C++ program and to release the mutex data later.
  • std::condition_variable – This thread class library is used to wait for and notify other program threads based on some particular condition in a C++ program.
  • std::async – This thread class library is used to launch asynchronous tasks in a C++ program.
  • std::future – This thread class library is used to display the results of asynchronous operations in a C++ program.

So, let’s learn in detail about std::async in C++, which is one of the most powerful features for managing asynchronous thread class operations in C++ programs.

std::async Thread Class Overview Concept in C++.

The std::async Thread class provides a higher-level interface for running user-defined tasks in asynchronous order in a C++ program. It creates a new thread in real time (or runs thread tasks asynchronously using another method) and returns a std::future object value, which can be used to display the result of the calculation after it completes.

Syntax of the std::async Thread class.

std::future<T> std::async(std::launch::policy, callable, arguments…);

Element of the std::async Thread class.

  • std::launch::policy – This C++ thread class determines whether user-defined thread class tasks can be run asynchronously (in a separate thread) or deferred (to run when needed).
  • std::launch::async – This C++ thread class forces a task to run in a separate thread.
  • std::launch::deferred – This C++ thread class forces a task to run in a separate thread until the result is needed (e.g., the get() function is called on a future).

C++ user std::launch::async | You can also pass the thread class to std::launch::deferred, allowing your system to decide whether to run the thread task asynchronously or defer it.

Element of std::launch::async | std::launch::deferred.

  • callable – This is a function, lambda, or any callable object method in a C++ thread class program.
  • arguments… – This is the arguments element for passing the callable object to a C++ thread class program.

Basic usage example of std::async in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countTotal(int p, int q) {

return p + q;

}

int main() {

// here we launch an asynchronous task thread method

std::future<int> output = std::async(std::launch::async, countTotal, 7, 4);

// here you can do other work while the task is running in the background process

std::cout << “Here main thread is open to do another task.” << std::endl;

// here we can get the output from the async thread task

std::cout << “The output of countTotal thread is – ” << output.get() << std::endl;

return 0;

}

Explanation of usage example of std::async in a C++ thread.

  • Here in this example, std::async(std::launch::async, countTotal, 7, 4); Launches the countTotal function asynchronously in a separate thread.
  • It returns a std::future<int> object with the value output, which will hold the output value after the calculation is complete.
  • To display the output, the output.get() function is called, which blocks until the task completes and returns the calculated value.

Remember, the get() function blocks in the current program until the asynchronous task completes and the output result is available.

Deferred execution with std::async in a C++ thread.

C++ users can also implement the std::async thread class library with deferred execution in a thread. This means that the current thread task will not start immediately. Instead, it will execute when the C++ user calls the get() function on the returned std::future.

Example of deferred execution in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countTotal(int p, int q) {

std::cout << “Here it is countTotal in the background thread” << std::endl;

return p + q;

}

int main() {

// here we launch an asynchronous thread task with deferred execution method

std::future<int> output = std::async(std::launch::deferred, countTotal, 9, 8);

// here the task has not yet started, it will only start when we call get() function method

std::cout << “Implementing another task in main thread” << std::endl;

// here the task is executed when we call output.get() function method

std::cout << “Output of countTotal thread is – ” << output.get() << std::endl;

return 0;

}

Explanation of deferred execution in a C++ thread.

  • In this example, std::async(std::launch::deferred, …) creates a deferred asynchronous task in the program. The thread task does not execute immediately. It will only run when the get() function is called on the returned std::future.
  • C++ users can see that the calculation is not completed until the get() function is called.

Handling exceptions in std::async in a C++ thread.

If a function launched with std::async in a C++ program throws an exception error, C++ users can catch that exception by calling the get() function on the std::future object.

Example of exception handling in std::async in a C++ thread.

#include <iostream>

#include <thread>

#include <future>

int countDivision(int p, int q) {

if (q == 0) {

throw std::runtime_error(“Value is Division by zero is not allowed”);

}

return p/q;

}

int main() {

// here the launch an asynchronous thread task that may throw an exception

std::future<int> output = std::async(std::launch::async, countDivision, 7, 0);

try {

// here we call get() function method to retrieve the output or throw the exception

std::cout << “The output of division is – ” << output.get() << std::endl;

}

catch (const std::exception& e) {

std::cout << “Here it caught the thread exception – ” << e.what() << std::endl;

}

return 0;

}

Explanation of exception handling in std::async in a C++ thread.

  • In this example, the divide function tests the division value by zero and throws a std::runtime_error exception error if the denominator is zero.
  • When the output.get() function method is called, the exception is re-thrown from the asynchronous task and caught in the main thread’s catch block.

Leave a Reply