Parallel.Invoke(Sub() WalkTree(node.Left), Sub() WalkTree(node.Right))
End Sub
24. Build and run the application. 25. You should observe that the employees in the tree are no longer processed in the same order and that several nodes start processing before others have completed. Also note that it took less time to walk the entire tree. Figure 6 Output from a parallel tree walker
Note: The Invoke() method schedules each call to WalkTree() individually, based on core availability. This means that the tree will not necessarily be walked in a predictable manner. Again, keep this in mind as you design your code.
Next Step: Exercise 2: Create and Run Parallelized Tasks
Exercise 2: Create and Run Parallelized Tasks The Parallel Extensions library provides a Task class that can be used to execute work items in parallel, across multiple cores. Basically, you can think of a Task object as a lightweight unit of work that might be scheduled to run in parallel to other units, if the TaskManager decides it is necessary. As Task objects are created you need to supply them with a delegate or lambda statement containing the logic to execute. Then the TaskManager, which is the real heart of the Parallel Extensions library, will schedule the Task to execute, possibly on a different thread running on a different core. Note: To verify that each step is correctly performed, it is recommended to build the solution at the end of each task.
Task 1 – Natively Running Parallelized Tasks 1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010. 2. Open the solution file ParallelExtLab.sln located under Source\Ex02-CreateAndRunParallelizedTasks\begin (choosing the folder that matches the language of your preference). Optionally, you can continue working with the solution you created in the previous exercise. 3. Replace the current method calls from Main(), with a call to Ex2Task1_NativeParallelTasks() method. C# static void Main(string[] args) { ... // Methods to call
|