简体   繁体   English

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

[英]What's the difference between parallel_for_each and parallel_for in MSVC's Concurrency Runtime?

parallel_for_each is of the form: parallel_for_each的形式如下:

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);

but parallel_for is also of the similar form: parallel_for也是类似的形式:

Concurrency::parallel_for(start_value, end_value, function_object);

so what is the difference between Concurrency::parallel_for and Concurrency::parallel_for_each algorithms used in programming for multiple cores? 那么在多核编程中使用的Concurrency::parallel_forConcurrency::parallel_for_each算法有什么区别?

I don't know what library you're talking about, but it looks like this one takes iterators: 我不知道你在谈论什么库,但它看起来像是一个迭代器:

Concurrency::parallel_for_each(start_iterator, end_iterator, function_object);

And likely has the same effect as this (although not necessarily in the same order): 并且可能具有与此相同的效果(尽管不一定按相同顺序):

for(sometype i = start_iterator; i != end_iterator; ++i) {
    function_object(*i);
}

For example: 例如:

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);

The other one takes values, so most likely it has a similar effect to this (but again, no guaranteed order): 另一个取值,所以很可能它具有类似的效果(但同样,没有保证的顺序):

for(sometype i = start_value; i != end_value; ++i) {
    function_object(i);
}

Try running this: 试试这个:

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;
}

EDIT: You can find confirmation of these behaviors in the Parallel Algorithm references . 编辑:您可以在并行算法参考中找到这些行为的确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM