Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Parallel execution with algorithms and execution policies
I've just learned about algorithms, ranges and views (from a book), including execution policy such as std::execution::par and I naively tried:
import std;
int main() {
auto values {std::ranges::views::iota(0, 10)};
std::for_each(
std::execution::par,
std::begin(values), std::end(values),
[] (const auto value) {
std::println("value: {}, thread: {}",
value, std::this_thread::get_id());
}
);
return 0;
}
According to std::this_thread::get_id(), all iterations are handled by the same thread.
Does the compiler consider this a too simple task to warrant the overhead of setting up multiple threads or are there other preconditions to be met?
I am compiling with:
g++ -std=c++26 -O2 -Wall -fmodules -fsearch-include-path bits/std.cc -ltbb parallel.cpp -o parallel
2 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
The following users marked this post as Works for me:
| User | Comment | Date |
|---|---|---|
| dode | (no comment) | Aug 17, 2026 at 21:18 |
I did some experiments, maybe it helps:
When building with modules (I don't have much experience with modules), I couldn't find any symbol for tbb when using nm, when I replaced modules with #includes, tbb was used, task_arenas to be more specific.
Using perf record -s ./parallel I could confirm, that the module version doesn't use tbb or threads, but using includes, 8 threads (my core count × 2) are used for 100 inputs.
Here is my code:
#include <execution>
#include <print>
#include <ranges>
#include <thread>
int main() {
auto values{std::ranges::views::iota(0, 100)};
std::for_each(
std::execution::par,
std::begin(values), std::end(values),
[] (const auto value) {
std::println("value: {}, thread: {}",
value, std::this_thread::get_id());
}
);
return 0;
}
and my output:
- TBB: 2023.1
$ g++ --version
g++ (GCC) 16.1.1 20260728
$ g++ -std=c++26 -O2 -Wall -ltbb parallel.cpp -o parallel
$ ./parallel
value: 0, thread: 140232593692544
value: 1, thread: 140232593692544
value: 2, thread: 140232593692544
value: 3, thread: 140232593692544
value: 4, thread: 140232593692544
value: 5, thread: 140232593692544
value: 6, thread: 140232593692544
value: 7, thread: 140232593692544
value: 8, thread: 140232593692544
value: 9, thread: 140232593692544
value: 10, thread: 140232593692544
value: 11, thread: 140232593692544
value: 12, thread: 140232593692544
value: 13, thread: 140232593692544
value: 14, thread: 140232593692544
value: 15, thread: 140232593692544
value: 16, thread: 140232593692544
value: 17, thread: 140232593692544
value: 18, thread: 140232593692544
value: 19, thread: 140232593692544
value: 20, thread: 140232593692544
value: 21, thread: 140232593692544
value: 22, thread: 140232593692544
value: 23, thread: 140232593692544
value: 24, thread: 140232593692544
value: 25, thread: 140232593692544
value: 26, thread: 140232593692544
value: 50, thread: 140232580118208
value: 51, thread: 140232580118208
value: 52, thread: 140232580118208
value: 27, thread: 140232593692544
value: 28, thread: 140232593692544
value: 29, thread: 140232593692544
value: 30, thread: 140232593692544
value: 31, thread: 140232593692544
value: 53, thread: 140232580118208
value: 54, thread: 140232580118208
value: 55, thread: 140232580118208
value: 56, thread: 140232580118208
value: 57, thread: 140232580118208
value: 58, thread: 140232580118208
value: 59, thread: 140232580118208
value: 60, thread: 140232580118208
value: 61, thread: 140232580118208
value: 62, thread: 140232580118208
value: 63, thread: 140232580118208
value: 64, thread: 140232580118208
value: 65, thread: 140232580118208
value: 66, thread: 140232580118208
value: 67, thread: 140232580118208
value: 68, thread: 140232580118208
value: 69, thread: 140232580118208
value: 32, thread: 140232593692544
value: 33, thread: 140232593692544
value: 34, thread: 140232593692544
value: 87, thread: 140232567523008
value: 35, thread: 140232593692544
value: 36, thread: 140232593692544
value: 70, thread: 140232580118208
value: 37, thread: 140232575919808
value: 74, thread: 140232580118208
value: 75, thread: 140232571721408
value: 71, thread: 140232489952960
value: 76, thread: 140232571721408
value: 78, thread: 140232580118208
value: 79, thread: 140232580118208
value: 84, thread: 140232494151360
value: 85, thread: 140232494151360
value: 86, thread: 140232494151360
value: 43, thread: 140232494151360
value: 44, thread: 140232494151360
value: 45, thread: 140232494151360
value: 46, thread: 140232494151360
value: 47, thread: 140232494151360
value: 48, thread: 140232494151360
value: 49, thread: 140232494151360
value: 40, thread: 140232494151360
value: 41, thread: 140232494151360
value: 81, thread: 140232485754560
value: 38, thread: 140232575919808
value: 39, thread: 140232575919808
value: 72, thread: 140232489952960
value: 93, thread: 140232575919808
value: 73, thread: 140232593692544
value: 83, thread: 140232593692544
value: 98, thread: 140232593692544
value: 99, thread: 140232593692544
value: 90, thread: 140232593692544
value: 91, thread: 140232593692544
value: 92, thread: 140232593692544
value: 95, thread: 140232593692544
value: 80, thread: 140232580118208
value: 82, thread: 140232485754560
value: 94, thread: 140232575919808
value: 88, thread: 140232567523008
value: 89, thread: 140232567523008
value: 42, thread: 140232494151360
value: 77, thread: 140232571721408
value: 96, thread: 140232489952960
value: 97, thread: 140232489952960
0 comment threads
I can’t comment on mavieth’s answer, but I think their conclusion is correct; the libstdc++ std module doesn’t use the oneTBB backend.
It seems like the problem is that oneTBB, when included as headers, injects translation-unit-local stuff into the module purview… which is not allowed.
oneTBB is modularized (import tbb;, see the third line in the table), so if you wanted to use it in a modules context, you could… but you’d have to use the actual oneTBB API, not the standard API.
We will probably have to wait for libstdc++ and oneTBB to sort out their issues before libstdc++ can use oneTBB as a back-end during a modules build.

0 comment threads