I wanted to parallelize a for loop and found out about std::for_each as well as its execution policies. Surprisingly it didn't parallelize when using GCC:
#include <iostream>
#include <algorithm>
#include <execution>
#include <chrono>
#include <thread>
#include <random>
int main() {
    std::vector<int> foo;
    foo.reserve(1000);
    for (int i = 0; i < 1000; i++) {
        foo.push_back(i);
    }
    std::for_each(std::execution::par_unseq,
                  foo.begin(), foo.end(),
                  [](auto &&item) {
                      std::cout << item << std::endl;
                      std::random_device dev;
                      std::mt19937 rng(dev());
                      std::uniform_int_distribution<std::mt19937::result_type> dist6(10, 100);
                      std::this_thread::sleep_for(std::chrono::milliseconds(dist6(rng)));
                      std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
                  });
}
This code still runs sequentially.
Using MSVC the code is parallelized and finishes much quicker.
GCC:
$ gcc --version
gcc (Ubuntu 10.1.0-2ubuntu1~18.04) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MSVC:
>cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29112 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]
CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(ParallelTesting)
set(CMAKE_CXX_STANDARD 20)
add_executable(ParallelTesting main.cpp)
Is there anything specific I need to do to enable parallelization with GCC as well?
ldd output of my binary:
$ ldd my_binary
    linux-vdso.so.1 (0x00007ffe9e6b9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f79efaa0000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f79ef881000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f79ef4ad000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f79ef295000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f79eeea4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f79f041a000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f79eeb06000)
The debug and release version of the binary overall have the same ldd output.
 
    