Skip to content

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.

fea

The supported algorithms include:

all std

https://en.cppreference.com/w/cpp/experimental/parallelism

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.

https://gcc.gnu.org/gcc-9/changes.html

Among other things, it mentions one very important piece of information: tbb support is required.

tbb req

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.

https://github.com/oneapi-src/oneTBB

The other is a tbb written by someone else, simply called tbb.

https://github.com/wjakob/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:

mkdir build && cmake ..
make -j8
make install

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:

std::sort(begin, end, comp);

and now becomes:

std::sort(exe_policy, begin, end, comp);

You only need to add an execution policy, and there are three options:

  1. std::execution::seq (sequential execution):
  2. This is the default execution policy, which means that if you do not explicitly specify an execution policy, the algorithm will use sequential execution.
  3. The algorithm processes the elements one after another in order, with no parallelism involved.
  4. std::execution::par (parallel execution):
  5. Using this execution policy enables parallel execution. The algorithm will try to process the elements in parallel, making full use of multi-core processors.
  6. std::execution::par_unseq (parallel and vectorized execution):
  7. 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.
  8. Unlike std::execution::par, par_unseq permits vectorization on top of parallel processing, thus making fuller use of the hardware's parallelism and vectorization capabilities.

2. Header level

You must include:

#include <algorithm>
#include <execution>

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:

BENCHMARK(BM_ParallelSort)->Arg(16384)->Arg(16384000)->Arg(163840000);  

The test results below use tbb's default thread scheduling policy.

Comparison group 1: parallel Sort / serial Sort detailed data and speedup

sort parallel

sort parallel sp

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

merge parallel

merge parallel sp

tbb documentation:

https://oneapi-src.github.io/oneTBB/index.html

To set the number of threads, you can do the following:

#include <tbb/tbb.h>

tbb::task_scheduler_init init(16);

Comments