0

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)
user2559936
  • 97
  • 3
  • 6

1 Answers1

1

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

Santosh-Intel
  • 156
  • 2
  • 6
  • I will test with ubuntu 20.04 in near term with production server (i9 10th gen), and reply here at my laptop it didn't work even with your code example (it's a i7 10th gen) – user2559936 Nov 08 '21 at 21:59