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

发布于 2024-12-21 13:52:00 字数 403 浏览 4 评论 0原文

parallel_for_each 的形式为:

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

parallel_for 的形式也类似:

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

那么 Concurrency::parallel_for之间有什么区别多核编程中使用的 Concurrency::parallel_for_each 算法?

parallel_for_each is of the form:

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

but parallel_for is also of the similar form:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-12-28 13:52:00

我不知道你在说什么库,但看起来这个库需要迭代器:

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

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

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

例如:

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

另一个库需要值,所以最有可能它具有与此类似的效果(但同样,没有保证顺序):

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

尝试运行此:

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

编辑:您可以在 并行算法参考

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文