You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
5
5
helpviewer_keywords: ["writing a parallel_for_each loop [Concurrency Runtime]", "parallel_for_each function, example"]
6
-
ms.assetid: fa9c0ba6-ace0-4f88-8681-c7c1f52aff20
7
6
---
8
-
# How to: Write a parallel_for_each Loop
7
+
# How to: Write a `parallel_for_each` Loop
9
8
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.
11
10
12
11
## Example
13
12
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.
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.
19
18
20
19
```Output
21
20
serial version:
22
21
found 17984 prime numbers
23
-
took 6115 ms
22
+
took 125 ms
24
23
25
24
parallel version:
26
25
found 17984 prime numbers
27
-
took 1653 ms
26
+
took 63 ms
28
27
```
29
28
30
29
## Compiling the Code
@@ -35,9 +34,9 @@ To compile the code, copy it and then paste it in a Visual Studio project, or pa
35
34
36
35
## Robust Programming
37
36
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).
0 commit comments