Last updated:2026-10-12
C++17 Parallel STL¶
1. Support status across compilers¶
Support for the parallel algorithms library in different compilers is as follows:
https://en.cppreference.com/w/cpp/compiler_support/17#C.2B.2B17_library_features
As you can see, it has been supported since gcc9, while clang's support is still incomplete; I tested it locally myself and it is indeed not supported.

The supported algorithms include:

2. Requirements of the parallel algorithms library¶
The parallel algorithms library can be used from gcc9 onward; let's take a look at the gcc-9 documentation below.
Among other things, it mentions one very important piece of information: tbb support is required.

TBB is a C++ library for parallel programming developed by Intel. TBB aims to simplify the development of parallel software on multi-core processors, providing a set of abstractions and models that make it easier for programmers to implement parallel algorithms.
1.To use the parallel algorithms library in C++17, tbb must be installed first — so what happens if tbb is not installed?
From my own testing, it compiles and runs just fine, only without any speedup; the result is the same as running the original non-parallel version.
2.How do you install TBB?
One option is the official tbb, called oneTBB.
The other is a tbb written by someone else, simply called tbb.
The difference is that the latter adds a CMake-based build system.
The official one now supports cmake too, so I recommend going with the official one.
Building it is also very simple:
3. How to use it¶
Compared with traditional STL usage, what has changed?
1. Interface level
It is actually quite simple. For example, sort used to be:
and now becomes:
You only need to add an execution policy, and there are three options:
std::execution::seq(sequential execution):- This is the default execution policy, which means that if you do not explicitly specify an execution policy, the algorithm will use sequential execution.
- The algorithm processes the elements one after another in order, with no parallelism involved.
std::execution::par(parallel execution):- Using this execution policy enables parallel execution. The algorithm will try to process the elements in parallel, making full use of multi-core processors.
std::execution::par_unseq(parallel and vectorized execution):- This execution policy allows the algorithm to vectorize while running in parallel, which means the SIMD (Single Instruction, Multiple Data) instruction sets can be used for parallel vectorized operations.
- Unlike
std::execution::par,par_unseqpermits vectorization on top of parallel processing, thus making fuller use of the hardware's parallelism and vectorization capabilities.
2. Header level
You must include:
If you use any tbb functionality, you can include the tbb headers, for example: to set the number of threads.
3. Compilation level
When compiling, you need to specify the linking information, for example:
g++ -O3 -std=c++17 -Wall -Wextra -pedantic -I/usr/local/include/tbb -L /usr/local/lib/ -o main.out main.cc -ltbb
4. Performance testing¶
I have been working on sort recently and wanted to see how merge compares with sort in terms of performance, so I wrote a benchmark with the following logic:
-
Serial sort vs parallel sort (par policy)
-
Serial merge vs parallel merge (par policy)
Data sizes:
The test results below use tbb's default thread scheduling policy.
Comparison group 1: parallel Sort / serial Sort detailed data and speedup


Comparison group 2: parallel Merge / serial Merge detailed data and speedup


tbb documentation:
To set the number of threads, you can do the following:
Related Topics¶
- C++ Concurrency in Action Reading Guide | C++那些事
- C++ Deadlock Troubleshooting | C++那些事
- C++ SIMD Vectorization | C++那些事
- C++ False Sharing and Cache Lines | C++那些事
- GDB Multi-Threaded All-Stop Debugging | C++那些事
- Helgrind Concurrency Error Detection | C++那些事
- libev Event Loop Internals | C++那些事
- C++ std::thread Implementation Internals | C++那些事
- C++ std::thread and jthread | C++那些事
- Getting Started with C++ Multithreading | C++那些事