Overview
Multithreading is a programming technique that allows a single process to execute multiple threads concurrently. This allows a program to perform multiple tasks simultaneously, improving the performance and responsiveness of the program. In C++, the std::thread
class, part of the C++11 standard library, is used to implement multithreading.
To use concurrency in C++, you will need to use the <thread>
header, which provides the std::thread
class and other related types and functions for creating and managing threads. Here’s a simple example of how to create and start a new thread:
This program creates a new std::thread
object, passing the function foo
as the argument to the thread’s constructor. The join
member function blocks the calling thread (in this case, the main thread) until the new thread has completed execution.
You can also use the std::async
function to run a function asynchronously and get a std::future
object that can be used to retrieve the result of the function when it becomes available. For example:
This program creates a new asynchronous task that executes the foo
function and returns a std::future
object that can be used to retrieve the result of the function when it becomes available. The get
member function of the std::future
object blocks the calling thread until the result is available, and then returns the result.
Basic examples
Example 1: Passing arguments to a thread function
In this example, we pass two arguments to the thread function:
When the foo
function is executed by the new thread, it will receive the arguments 10
and "Hello"
.
Example 2: Using std::move to transfer ownership of a thread
In this example, we use std::move
to transfer ownership of a thread object to a new thread:
The t1
thread object is created and starts executing the foo
function. The t2
thread object is then created by moving the t1
object using std::move
. This transfers ownership of the thread to the t2
object, and the t1
object is left in a moved-from state and is no longer associated with any thread. The t2
object can then be used to manage the thread.
Example 3: Detaching a thread
In this example, we create a thread and detach it from the main thread of execution:
The detach
member function of the std::thread
object detaches the thread from the main thread of execution, allowing it to run independently. The main thread can then continue execution without waiting for the detached thread to complete.
Example 4: Passing arguments by reference to std::thread
In this example, we pass an argument by reference to thread function:
More algorithmic examples
There are many other algorithms that can be implemented as multithreaded versions to take advantage of concurrency, including the following:
-
Sorting algorithms: Many sorting algorithms, such as merge sort and quick sort, can be implemented as multithreaded versions to improve performance on multi-core systems.
-
Matrix multiplication: Matrix multiplication can be implemented as a multithreaded algorithm to take advantage of parallelism, particularly for large matrices.
-
Graph algorithms: Many graph algorithms, such as breadth-first search and depth-first search, can be implemented as multithreaded versions to improve performance on multi-core systems.
-
Search algorithms: Some search algorithms, such as binary search and linear search, can be implemented as multithreaded versions to improve performance on multi-core systems.