From 3d503852ad1c51094b02af8ddb351edf2a4077b9 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 23 May 2014 04:04:48 +0200 Subject: [PATCH] Threads and C# http://parallelpatterns.codeplex.com/ Chapter 6: Dynamic Task Parallelism --- Allgemeines/Threads/Threads/Chapter6.cs | 70 ++++++++++++++++++++++++++++++ Allgemeines/Threads/Threads/Program.cs | 12 +++++ Allgemeines/Threads/Threads/Threads.csproj | 1 + 3 files changed, 83 insertions(+) create mode 100644 Allgemeines/Threads/Threads/Chapter6.cs diff --git a/Allgemeines/Threads/Threads/Chapter6.cs b/Allgemeines/Threads/Threads/Chapter6.cs new file mode 100644 index 0000000..79ed287 --- /dev/null +++ b/Allgemeines/Threads/Threads/Chapter6.cs @@ -0,0 +1,70 @@ +using System; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +namespace Threads +{ + public class Chapter6 + { + /// + /// + /// Chapter 6. + /// Dynamic Task Parallelism + /// + /// Taken from http://msdn.microsoft.com/en-us/library/ff963551.aspx + /// + /// + + public void FillTree(int level, Tree node, Func function) + { + node.Data = function(); + node.Level = level; + + level = level - 1; + if (level == 0) + { + return; + } + + node.Left = new Tree(); + FillTree(level, node.Left, function); + node.Right = new Tree(); + FillTree(level, node.Right, function); + } + + public void SequentialWalk(Tree tree, Action action) + { + if (tree == null) + { + return; + } + + action(tree.Data, tree.Level); + this.SequentialWalk(tree.Left, action); + this.SequentialWalk(tree.Right, action); + } + + public void ParallelWalk(Tree tree, Action action) + { + if (tree == null) + { + return; + } + + var t1 = Task.Factory.StartNew(() => action(tree.Data, tree.Level)); + var t2 = Task.Factory.StartNew(() => ParallelWalk(tree.Left, action)); + var t3 = Task.Factory.StartNew(() => ParallelWalk(tree.Right, action)); + + Task.WaitAll(t1, t2, t3); + } + + public class Tree + { + public T Data { get; set; } + public int Level { get; set; } + public Tree Left { get; set; } + public Tree Right { get; set; } + } + } +} + diff --git a/Allgemeines/Threads/Threads/Program.cs b/Allgemeines/Threads/Threads/Program.cs index 37a927f..f9fb092 100644 --- a/Allgemeines/Threads/Threads/Program.cs +++ b/Allgemeines/Threads/Threads/Program.cs @@ -22,6 +22,18 @@ namespace Threads Chapter3.Test(); Chapter4.Test(); Chapter5.Test(); + Chapter6 chapter6 = new Chapter6(); + var tree = new Threads.Chapter6.Tree(); + var levels = 10; + chapter6.FillTree(levels, tree, () => 40); + + Console.WriteLine("Sequential Walk"); + chapter6.SequentialWalk(tree, (data, level) => Console.WriteLine("Level: {0} Data: {1}", level, data)); + + Console.WriteLine("Parallel Walk"); + // In my case the parallel walk is slower than the sequential one because the tasks are short and + // we waste more time creating new threads and synchronizing than running these simple tasks. + chapter6.ParallelWalk(tree, (data, level) => Console.WriteLine("Level: {0} Data: {1}", level, data)); } } } diff --git a/Allgemeines/Threads/Threads/Threads.csproj b/Allgemeines/Threads/Threads/Threads.csproj index e158fea..cd8f300 100644 --- a/Allgemeines/Threads/Threads/Threads.csproj +++ b/Allgemeines/Threads/Threads/Threads.csproj @@ -43,6 +43,7 @@ See: http://parallelpatterns.codeplex.com/ + \ No newline at end of file -- 2.1.4