Parallel processing 如何基于条件退出std::execution::par_unseq循环

Parallel processing 如何基于条件退出std::execution::par_unseq循环,parallel-processing,c++17,Parallel Processing,C++17,如果我的一个队列元素满足条件,我需要退出循环。放置中断会导致编译错误。并行执行循环的最佳方式是什么 //代码 std::deque<element> my_elements; std::for_each(std::execution::par_unseq, my_elements.begin(), my_elements.end(), [&](auto v) { if (v.attribute == something) { bre

如果我的一个队列元素满足条件,我需要退出循环。放置中断会导致编译错误。并行执行循环的最佳方式是什么

//代码

std::deque<element> my_elements;

std::for_each(std::execution::par_unseq, my_elements.begin(), my_elements.end(),
    [&](auto v) {

    if (v.attribute == something)
    {
        break;
    }
});
std::定义我的元素;
std::for_each(std::execution::par_unseq,my_elements.begin(),my_elements.end(),
[&](自动v){
如果(v.attribute==某物)
{
打破
}
});

我敢肯定那是行不通的。为什么?因为
break
是一个静态范围的操作;i、 e,
中断
的目的地在编译时确定。将其放入lambda(lambda外部的循环)使编译器无法看到“中断到”的位置

示例w/o并行性:

    vector<int> v {1,2,3,4,5,6,7,8,9};
    std::for_each(v.begin(), v.end(),
        [](int i) { cout << i << ' '; if (i == 5) break; });
    cout << endl;
向量v{1,2,3,4,5,6,7,8,9};
std::for_each(v.begin(),v.end(),

[](国际一){满足条件后,是否有其他方法来取消其他线程。例如OpenMP 4.0中的OpenMP取消和取消点,遗憾的是MSVC编译器不支持这些点。在这里你不能得到你想要的。你必须为每个线程运行你自己的并行,或者使用常规的顺序for循环。快速查看一些源代码如果您使用的是针对英特尔TBB构建的正确版本的libc++,并且使用的是英特尔编译器,那么“支持提前退出”预处理器宏将被设置,
std::any_of
将提前退出并取消其他线程。谢谢。经过一点探索,我发现有std::any_of和std::find_的并行版本,如果它们符合我的要求
so.cpp:10:45: error: 'break' statement not in loop or switch statement
     [](int i) { cout << i << ' '; if (i == 5) break; });