Parallel Programming with Microsoft .Net : Dynamic Task Parallelism - Variations

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
12/2/2010 11:28:54 AM
Dynamic task parallelism has several variations.

1. Parallel While-Not-Empty

The examples shown so far in this chapter use techniques that are the parallel analogs of sequential depth-first traversal. There are also parallel algorithms for other types of traversals. These techniques rely on concurrent collections to keep track of the remaining work to be done. Here’s an example.

public static void ParallelWhileNotEmpty<T>(
IEnumerable<T> initialValues,
Action<T, Action<T>> body)
var from = new ConcurrentQueue<T>(initialValues);
while (!from.IsEmpty)
var to = new ConcurrentQueue<T>();
Action<T> addMethod = to.Enqueue;
Parallel.ForEach(from, v => body(v, addMethod));
from = to;

This method shows how you can use Parallel.ForEach to process an initial collection of values. While processing the values, additional values to process may be discovered. The additional values are placed in the to queue. After the first batch of values processes, the method starts processing the additional values, which may again result in more values to process. This process repeats until no additional values can be produced.

A method that walks a binary tree can use the ParallelWhileNot Empty method.

static void ParallelWalk4<T>(Tree<T> tree, Action<T> action)
if (tree == null) return;
ParallelWhileNotEmpty(new[] { tree }, (item, adder) =>
if (item.Left != null) adder(item.Left);
if (item.Right != null) adder(item.Right);

An example of a problem that would be an appropriate use of the ParallelWhileNotEmpty method is a website link checking tool. The walk task loads the initial page and searches it for links. Each link is checked and removed from the list, and additional links to unchecked pages from the same site are added to the list. Eventually, there are no more unchecked links and the application stops.

2. Task Chaining with Parent/Child Tasks

The TPL includes a task creation option named AttachedToParent. This option is used most frequently in code that uses the dynamic task parallelism pattern. The purpose of the AttachedToParent option is to link a subtask to the task that created it. In this case, the subtask is known as a child task and the task that created the child task is known as a parent task.

You use the AttachedToParent option in two situations. The first is when you want to link the status of a parent task to the status of its child tasks. The second is when you want to use the Microsoft® Visual Studio® development system debugger to see the parent/ child relationships. The order of execution is not changed by using the AttachedToParent option.

The following code shows an example.

static void ParallelWalk2<T>(Tree<T> tree, Action<T> action)
if (tree == null) return;
var t1 = Task.Factory.StartNew(
() => action(tree.Data),
var t2 = Task.Factory.StartNew(
() => ParallelWalk2(tree.Left, action),
var t3 = Task.Factory.StartNew(
() => ParallelWalk2(tree.Right, action),
Task.WaitAll(t1, t2, t3);

The AttachedToParent option affects the behavior of the parent task. If a parent task with at least one running child task finishes running for any reason, its Status property becomes WaitingForChildrenToComplete. Only when all of its attached children are no longer running will the parent task’s Status property transition to one of the three final states. Figure 1 illustrates this.

Figure 1. Life cycle of a task that has attached children

Exceptions from attached children are observed in the parent task.

To access the parent/child view in the Visual Studio debugger, right-click the row headings in the Parallel Tasks window, and then click Parent Child View.

Other -----------------
- Keyword Research Tools (part 7) - comScore Marketer
- Keyword Research Tools (part 6)
- Keyword Research Tools (part 5)
- Keyword Research Tools (part 4)
- Keyword Research Tools (part 3)
- Keyword Research Tools (part 2)
- Keyword Research Tools (part 1) - Keyword Research Data from the Engines
- The Art of SEO : Traditional Approaches: Domain Expertise, Site Content Analysis
- The Art of SEO : The Theory Behind Keyword Research
- jQuery 1.3 : Headline rotator (part 7)
- jQuery 1.3 : Headline rotator (part 6)
- jQuery 1.3 : Headline rotator (part 5) - Pause on hover
- jQuery 1.3 : Headline rotator (part 4) - The headline rotate function
- jQuery 1.3 : Headline rotator (part 3) - Setting up the rotator
- jQuery 1.3 : Headline rotator (part 2) - Retrieving the feed
- jQuery 1.3 : Headline rotator (part1) - Setting up the page
- Benchmarking Current Rankingstages of SEO
- First Stages of SEO : Benchmarking Current Rankings
- First Stages of SEO : Benchmarking Current Indexing Status
- First Stages of SEO : Assessing Historical Progress
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS