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
算法有什么区别?
最佳答案
我不知道你在说什么库,但看起来这个库需要迭代器:
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;
}
编辑:您可以在 Parallel Algorithm references 中找到对这些行为的确认。 .
关于c++ - MSVC 并发运行时中的 parallel_for_each 和 parallel_for 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499245/