Skip to content

Commit bcaa5d5

Browse files
authored
Merge pull request #4163 from MicrosoftDocs/main637986869138731474sync_temp
Repo sync for protected CLA branch
2 parents f1b89fe + 1ad8d85 commit bcaa5d5

File tree

2 files changed

+71
-60
lines changed

2 files changed

+71
-60
lines changed

docs/parallel/concrt/codesnippet/CPP/how-to-write-a-parallel-for-each-loop_1.cpp

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,78 @@
99
using namespace concurrency;
1010
using namespace std;
1111

12-
// Calls the provided work function and returns the number of milliseconds
13-
// that it takes to call that function.
12+
// Returns the number of milliseconds that it takes to call the passed in function.
1413
template <class Function>
1514
__int64 time_call(Function&& f)
1615
{
17-
__int64 begin = GetTickCount();
18-
f();
19-
return GetTickCount() - begin;
16+
__int64 begin = GetTickCount();
17+
f();
18+
return GetTickCount() - begin;
2019
}
2120

22-
// Determines whether the input value is prime.
21+
// Determines whether the input is a prime.
2322
bool is_prime(int n)
2423
{
25-
if (n < 2)
26-
return false;
27-
for (int i = 2; i < n; ++i)
28-
{
29-
if ((n % i) == 0)
30-
return false;
31-
}
32-
return true;
24+
if (n < 2)
25+
{
26+
return false;
27+
}
28+
29+
for (int i = 2; i < int(std::sqrt(n)) + 1; ++i)
30+
{
31+
if (n % i == 0)
32+
{
33+
return false;
34+
}
35+
}
36+
return true;
3337
}
3438

3539
int wmain()
3640
{
37-
// Create an array object that contains 200000 integers.
38-
array<int, 200000> a;
41+
// Create an array object that contains 200000 integers.
42+
array<int, 200000> a;
3943

40-
// Initialize the array such that a[i] == i.
41-
int n = 0;
42-
generate(begin(a), end(a), [&] {
43-
return n++;
44-
});
44+
// Initialize the array such that a[i] == i.
45+
int n = 0;
46+
generate(begin(a), end(a), [&]
47+
{
48+
return n++;
49+
});
4550

46-
LONG prime_count;
47-
__int64 elapsed;
51+
// Use the for_each algorithm to count, serially, the number
52+
// of prime numbers in the array.
53+
LONG prime_count = 0L;
54+
__int64 elapsed = time_call([&]
55+
{
56+
for_each(begin(a), end(a), [&](int n)
57+
{
58+
if (is_prime(n))
59+
{
60+
++prime_count;
61+
}
62+
});
63+
});
64+
65+
wcout << L"serial version: " << endl
66+
<< L"found " << prime_count << L" prime numbers" << endl
67+
<< L"took " << elapsed << L" ms" << endl << endl;
4868

49-
// Use the for_each algorithm to count the number of prime numbers
50-
// in the array serially.
51-
prime_count = 0L;
52-
elapsed = time_call([&] {
53-
for_each (begin(a), end(a), [&](int n ) {
54-
if (is_prime(n))
55-
++prime_count;
56-
});
57-
});
58-
wcout << L"serial version: " << endl
59-
<< L"found " << prime_count << L" prime numbers" << endl
60-
<< L"took " << elapsed << L" ms" << endl << endl;
69+
// Use the parallel_for_each algorithm to count, in parallel, the number
70+
// of prime numbers in the array.
71+
prime_count = 0L;
72+
elapsed = time_call([&]
73+
{
74+
parallel_for_each(begin(a), end(a), [&](int n)
75+
{
76+
if (is_prime(n))
77+
{
78+
InterlockedIncrement(&prime_count);
79+
}
80+
});
81+
});
6182

62-
// Use the parallel_for_each algorithm to count the number of prime numbers
63-
// in the array in parallel.
64-
prime_count = 0L;
65-
elapsed = time_call([&] {
66-
parallel_for_each (begin(a), end(a), [&](int n ) {
67-
if (is_prime(n))
68-
InterlockedIncrement(&prime_count);
69-
});
70-
});
71-
wcout << L"parallel version: " << endl
72-
<< L"found " << prime_count << L" prime numbers" << endl
73-
<< L"took " << elapsed << L" ms" << endl << endl;
83+
wcout << L"parallel version: " << endl
84+
<< L"found " << prime_count << L" prime numbers" << endl
85+
<< L"took " << elapsed << L" ms" << endl << endl;
7486
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
---
2-
description: "Learn more about: How to: Write a parallel_for_each Loop"
3-
title: "How to: Write a parallel_for_each Loop"
4-
ms.date: "11/04/2016"
2+
description: "Learn more about how to write a parallel_for_each Loop"
3+
title: "How to: write a parallel_for_each Loop"
4+
ms.date: 09/12/2022
55
helpviewer_keywords: ["writing a parallel_for_each loop [Concurrency Runtime]", "parallel_for_each function, example"]
6-
ms.assetid: fa9c0ba6-ace0-4f88-8681-c7c1f52aff20
76
---
8-
# How to: Write a parallel_for_each Loop
7+
# How to: Write a `parallel_for_each` Loop
98

10-
This example shows how to use the [concurrency::parallel_for_each](reference/concurrency-namespace-functions.md#parallel_for_each) algorithm to compute the count of prime numbers in a [std::array](../../standard-library/array-class-stl.md) object in parallel.
9+
This example shows how to use the [`concurrency::parallel_for_each`](reference/concurrency-namespace-functions.md#parallel_for_each) algorithm to compute the count of prime numbers in a [`std::array`](../../standard-library/array-class-stl.md) object in parallel.
1110

1211
## Example
1312

14-
The following example computes the count of prime numbers in an array two times. The example first uses the [std::for_each](../../standard-library/algorithm-functions.md#for_each) algorithm to compute the count serially. The example then uses the `parallel_for_each` algorithm to perform the same task in parallel. The example also prints to the console the time that is required to perform both computations.
13+
The following example computes the count of prime numbers in an array two times. The example first uses the [`std::for_each`](../../standard-library/algorithm-functions.md#for_each) algorithm to compute the count serially. The example then uses the `parallel_for_each` algorithm to perform the same task in parallel. The example also prints to the console the time that is required to perform both computations.
1514

1615
[!code-cpp[concrt-parallel-count-primes#1](../../parallel/concrt/codesnippet/cpp/how-to-write-a-parallel-for-each-loop_1.cpp)]
1716

18-
The following sample output is for a computer that has four processors.
17+
The following sample output is for a computer that has four cores.
1918

2019
```Output
2120
serial version:
2221
found 17984 prime numbers
23-
took 6115 ms
22+
took 125 ms
2423
2524
parallel version:
2625
found 17984 prime numbers
27-
took 1653 ms
26+
took 63 ms
2827
```
2928

3029
## Compiling the Code
@@ -35,9 +34,9 @@ To compile the code, copy it and then paste it in a Visual Studio project, or pa
3534
3635
## Robust Programming
3736

38-
The lambda expression that the example passes to the `parallel_for_each` algorithm uses the `InterlockedIncrement` function to enable parallel iterations of the loop to increment the counter simultaneously. If you use functions such as `InterlockedIncrement` to synchronize access to shared resources, you can present performance bottlenecks in your code. You can use a lock-free synchronization mechanism, for example, the [concurrency::combinable](../../parallel/concrt/reference/combinable-class.md) class, to eliminate simultaneous access to shared resources. For an example that uses the `combinable` class in this manner, see [How to: Use combinable to Improve Performance](../../parallel/concrt/how-to-use-combinable-to-improve-performance.md).
37+
The lambda expression that the example passes to the `parallel_for_each` algorithm uses the `InterlockedIncrement` function to enable parallel iterations of the loop to increment the counter simultaneously. If you use functions such as `InterlockedIncrement` to synchronize access to shared resources, you can present performance bottlenecks in your code. You can use a lock-free synchronization mechanism, for example, the [`concurrency::combinable`](../../parallel/concrt/reference/combinable-class.md) class, to eliminate simultaneous access to shared resources. For an example that uses the `combinable` class in this manner, see [How to: Use combinable to improve performance](../../parallel/concrt/how-to-use-combinable-to-improve-performance.md).
3938

4039
## See also
4140

42-
[Parallel Algorithms](../../parallel/concrt/parallel-algorithms.md)<br/>
43-
[parallel_for_each Function](reference/concurrency-namespace-functions.md#parallel_for_each)
41+
[Parallel algorithms](../../parallel/concrt/parallel-algorithms.md)\
42+
[`parallel_for_each` Function](reference/concurrency-namespace-functions.md#parallel_for_each)

0 commit comments

Comments
 (0)