[ Pobierz całość w formacie PDF ]
.Using Synchronization to Coordinate TasksIn this section, we revisit the topic of synchronization primitives, this time using them to coordinateactivity between and amongst groups of Tasks.We want one group of Tasks (called the supervisors) to exert some direction over another group ofTasks (called the workers).A synchronization primitive is used to mediate between the two groups andallows them to communicate.The communication between the supervisors and the workers is limited totwo messages go and wait.129CHAPTER 4 % COORDINATING TASKSThe synchronization primitive keeps track of a condition.Worker Tasks check with the primitive tosee if the condition has been satisfied.If it has, they are told to go and will continue their work.If thecondition has not been satisfied, they are made to wait until it is.The details of the condition vary from one type of primitive to another, but what they all share incommon is that they are satisfied by when the supervisors signal the primitiveIn effect, the workers are waiting for signals from the supervisors channeled through thesynchronization primitive.Worker Tasks wait for the signals by calling the primitive s Wait() method (orWaitOne() for classic primitives).the Wait() method blocks (does not return) until the expected signalshave been received and the primitive condition has been satisfied.When a primitive tells a worker Taskthat has been waiting that it may now proceed because the condition has been satisfied, the primitive issaid to wake, notify, or release the waiting Task.You could write your own code to allow supervisors to signal workers, but I recommend that youdon t.First, writing synchronization primitives correctly is very hard , and the odds are that you willmake mistakes unless you are very experienced in parallel programming.Second, the primitivesincluded in the.NET class library cover the vast majority of situations that parallel programmersencounter and are implemented using a broadly consistent interface.If the type of condition you needyour primitive to manage should change, it is a relatively simple thing to switch from one standardprimitive to another.Table 4-5 summarizes the main uses for the most commonly used primitives.Table 4-5.Coordinating TasksProblem Solution ListingImplement a cooperative multi-phase Use the System.Threading.Barrier class.4-12, 4-13,algorithm.and 4-14Coordinate Tasks so that multiple Use the System.Threading.CountDownEvent class.4-15supervisors signal the primitivebefore the workers are releasedCoordinate Tasks so that one signal Use the System.Threading.ManualResetEventSlim 4-16releases all workers.class.Coordinate Tasks so that one signal Use the System.Threading.AutoResetEvent class.4-17releases one worker.Coordinate Tasks so that one signal Use the System.Threading.SemaphoreSlim class.4-18releases a specified number ofworkers.For some primitives, both classic and lightweight versions are available.The lightweight versionshave names that end with slim, such as ManualResetEventSlim.The lightweight versions have betterperformance characteristics for most uses when compared with the classic versions, because a call toWait() on a lightweight primitive is initially handled by spinning, which is ideally suited to short waitingperiods.See Chapter 2 for a description of spinning.The lightweight versions also support waiting usinga CancellationToken, which is something that I find endlessly useful.I use the lightweightimplementations for preference and recommend that you do the same.130CHAPTER 4 % COORDINATING TASKSBarrierWhen using the System.Threading.Barrier primitive, the supervisors and the workers are the sameTasks, making Barrier useful for coordinating Tasks performing a multiphase parallel algorithm.Multiphase algorithms are broken down into several stages (called phases), where all of the Tasksparticipating in the work must reach the end of one phase before the next one can begin.This behavioris useful if the results produced in one phase are required as inputs for the next
[ Pobierz całość w formacie PDF ]