在MSVC的并发运行时中parallel_for_each和parallel_for有什么区别?

Vis*_*iva 4 c++ concurrency multicore visual-studio-2010 concurrent-programming

parallel_for_each 具有以下形式:

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);
Run Code Online (Sandbox Code Playgroud)

parallel_for也有类似的形式:

Concurrency::parallel_for(start_value, end_value, function_object);
Run Code Online (Sandbox Code Playgroud)

那么究竟是什么之间的差异Concurrency::parallel_for,并Concurrency::parallel_for_each在编程中使用多个内核算法?

Rei*_*ica 7

我不知道你在谈论什么库,但它看起来像是一个迭代器:

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);
Run Code Online (Sandbox Code Playgroud)

并且可能具有与此相同的效果(尽管不一定按相同顺序):

for(sometype i = start_iterator; i != end_iterator; ++i) {
    function_object(*i);
}
Run Code Online (Sandbox Code Playgroud)

例如:

void do_stuff(int x) { /* ... */ }
vector<int> things;
// presumably calls do_stuff() for each thing in things
Concurrency::parallel_for_each(things.begin(), things.end(), do_stuff);
Run Code Online (Sandbox Code Playgroud)

另一个取值,所以很可能它具有类似的效果(但同样,没有保证的顺序):

for(sometype i = start_value; i != end_value; ++i) {
    function_object(i);
}
Run Code Online (Sandbox Code Playgroud)

试试这个:

void print_value(int value) {
    cout << value << endl;
}

int main() {
    // My guess is that this will print 0 ... 9 (not necessarily in order)
    Concurrency::parallel_for(0, 10, print_value);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:您可以在并行算法参考中找到这些行为的确认.