【问题标题】:CMake Release and Debug running different std::for_each single thread / multi thread c++17CMake Release和Debug运行不同的std :: for_each单线程/多线程c ++ 17
【发布时间】:2021-11-29 21:02:18
【问题描述】:

您好,我正在执行 std::foreach ,例如:

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

当我使用 DEBUG 版本(cmake -DCMAKE_BUILD_TYPE=Debug ..)构建时,它使用多线程运行(我可以使用 htop 检查它) 但是当我使用 RELEASE 版本(cmake -DCMAKE_BUILD_TYPE=Release ..)构建时,它以单线程运行

有什么我必须检查的吗?

我正在使用 c++17 / g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 与

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

【问题讨论】:

    标签: foreach c++17 std tbb gcc9


    【解决方案1】:

    我尝试在具有 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; }
    );
    
    }
    

    我的 CMakeLists.txt 如下:

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

    我没有遇到您上面提到的任何此类行为。它使用“发布和调试”模式以多线程方式运行。请参考以下截图:

    谢谢, 桑托什

    【讨论】:

    • 我将在短期内使用生产服务器(i9 第 10 代)使用 ubuntu 20.04 进行测试,并在我的笔记本电脑上回复,即使使用您的代码示例也无法正常工作(它是 i7 第 10 代)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2018-09-27
    • 2023-03-27
    相关资源
    最近更新 更多