CMake Release and Debug running different std::for_each single thread / multi thread c++17

208 Views Asked by At

Hello I'm executing a std::foreach like:

std::for_each(
    std::execution::par_unseq,
    vector_of_int.begin(), 
    vvector_of_int.end(),
    [&captured_variables](auto &v) { Execute(v, captured_variables); }
);

when i build with DEBUG version (cmake -DCMAKE_BUILD_TYPE=Debug ..), it runs with multi thread (i can check it with htop) but when i build with RELEASE version (cmake -DCMAKE_BUILD_TYPE=Release ..), it runs with single thread

its there something i must check?

i'm using c++17 / g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 with

cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 17)
target_link_libraries(MyCode pthread tbb)
1

There are 1 best solutions below

1
On

I tried to run the below sample program on my machine having g++ (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0.

#include<iostream>
#include<vector>
#include<execution>
int main()

{
  std::vector<int> vector_of_int;

  //std::mutex m;
for(int i=0;i<100000;i++)
{
        vector_of_int.push_back(i);
}
 std::for_each(
    std::execution::par_unseq,
    vector_of_int.begin(),
    vector_of_int.end(),
    [&](auto &v) { std::cout<<v<<"th element of vector is-->"<<vector_of_int[v]<<std::endl; }
);

}

My CMakeLists.txt is as below:

cmake_minimum_required(VERSION 3.1)
project (sample)
add_executable(sample sample.cpp)
set(CMAKE_CXX_STANDARD 17)
target_link_libraries(sample pthread tbb)

I didn't face any such behavior that you mentioned above. It is running in a multi-threaded fashion using both "Release & Debug" modes. Please refer to the screenshots below:

Thanks, Santosh