Anon
Anon

Reputation: 1354

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

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?

Upvotes: 4

Views: 6632

Answers (1)

Brendan Long
Brendan Long

Reputation: 54242

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.

Upvotes: 7

Related Questions