S

scaling applications, 3

Select( ) method, 280

selective continuations, 110-111, 115

SelectMany( ) method, 280

SemaphoreSlim class, 143-146

sequential execution, 4, 51-52

sequential filtering, 249-250

sequential loops, 173-174

Set As Startup Project option, 5-6

SetObserved( ) method, 43

sharedQueue.Dequeue( ) method, 88

sharedStartIndex variable, 207

sharing data, 49-107

basic synchronization primitives, 61-87

configuring interprocess synchronization, 76

declarative synchronization, 78-79

interlocked operations, 67-69

locking and monitoring, 62-65

reader-writer locks, 79-87

spin locking, 70-72

wait handles and Mutex class, 72-76

challenges with, 50-52

common problems and causes, 100107

lock acquisition order, 103-104

multiple locks, 101-102

orphaned locks, 105-107

unexpected mutability, 100

executing immutably, 52-53

in isolation, 53-59

sequentially, 52

synchronizing, 59-61

ShouldExitCurrentIteration method, 184, 187

Signal( ) method, 136-137, 139

SignalAndWait( ) method, 131-132, 134135

SignalAndWait(CancellationToken) method, Barrier class, 131

SignalAndWait(Int32, CancellationToken) method, Barrier class, 131

SignalAndWait(Int32) method, Barrier class, 131

SignalAndWait(TimeSpan,

CancellationToken) method, Barrier class, 131

SignalAndWait(TimeSpan) method, Barrier class, 131

Signal(int) method, CountDownEvent class, 137

simple continuations, 111-113

single lock object, 65

single Task, waiting for, 31-32

sleeping, 26-28

SleepWait( ) method, 26

Solution Explorer window, 5

Source property, 37

speculative processing selection, 285-287

speculative caching, 288-289

spin locking, 70-72

SpinCount member, ManualResetEvent class, 139

SpinLock class, 70, 72

spinning excessive, 47-48

spin waiting, 29-30

StartProcessing( ) method, 292 static partitioners, 195 StaticPartitioner class, 243 Status column, 264 SteppedIterator( ) method, 180 Stop( ) method, 183-184 stopping parallel loops, 183-187 StopProcessing( ) method, 292 Stopwatch class, 4 supervisors, 129

SupportsDynamicPartitions property, 206

synchronization, 51

basic primitives, 61-87

configuring interprocess synchronization, 76

declarative synchronization, 78-79

interlocked operations, 67-69

locking and monitoring, 62-65

reader-writer locks, 79-87

spin locking, 70-72

wait handles and Mutex class, 72-76

declarative, 78-79

interprocess, configuring, 76

using readily, 252

using sparingly, 252

using to coordinate tasks, 129-146

AutoResetEvent class, 141-142

Barrier class, 131-135

CountDownEvent class, 136-139

ManualResetEventSlim class, 139

SemaphoreSlim class, 143-146

Synchronization attribute, 78-79

synchronization handles, 72

Synchronized method, 97-98

synchronizing execution, 59-61

System.Action( ) method, 9, 131-132, 274

System.Action<object> instance, 11

System.Action<Task> argument, 111-112

System.Action(Barrier) method, 132

System.AggregateException class, 36

System.Array.Sort( ) method, 272

System.Collection.IEnumerable interface, 177

System.Collections.Concurrent namespace, 88, 94, 200, 216

System.Collections.Concurrent.BlockingC ollection class, 147

System.Collections.Concurrent.Concurren tBag class, 94

System.Collections.Concurrent.Concurren tDictionary class, 95

System.Collections.Concurrent.Concurren tQueue class, 91

System.Collections.Concurrent.Concurren tStack class, 91-92

System.Collections.Concurrent.IProducer ConsumerCollection interface, 149

System.Collections.Concurrent.Partitioner class, 198

System.Collections.Generic namespace, 99

System.Collections.Generic.IEnumerable< T> interface, 177

System.Collections.Generic.Queue<int> class, 87

System.Collections.Queue class, 97

System.ContextBoundObject class, 78

System.Diagnostics.Debugger class, 268

System.Diagnostics.Stopwatch class, 253

System.Exception class, 115

System.InvalidOperationException class, 88

System.Linq namespace, 221

System.Runtime.Remoting.Contexts namespace, 78

System.Threading namespace, 8

System.Threading.AutoResetEvent class, 130, 141

System.Threading.Barrier class, 130-131

System.Threading.CancellationTokenSour ce.CreateLinkedTokenSource( ) method, 23

System.Threading.CountDownEvent class, 130,136-137

System.Threading.Interlocked class, 62, 67, 191

System.Threading.LockRecursionExceptio n class, 72

System.Threading.ManualResetEvent class, 139

System.Threading.ManualResetEventSlim class, 130, 139

System.Threading.Monitor class, 63

System.Threading.Monitor.TryEnter method, 65

System.Threading.Montor class, 62

System.Threading.Mutex class, 62, 72

System.Threading.OperationCanceledExce ption method, 17

System.Threading.ReaderWriter class, 80

System.Threading.ReaderWriterLockSlim class, 79

System.Threading.Semaphore class, 143

System.Threading.SemaphoreSlim class, 130, 143-144

System.Threading.SpinLock class, 62, 70, 72

System.Threading.Tasks.ConcurrentBag class, 89

System.Threading.Tasks.ConcurrentDictio nary class, 89

System.Threading.Tasks.ConcurrentQueu e class, 89

System.Threading.Tasks.ConcurrentStack class, 89

System.Threading.Tasks.Parallel class, 175

System.Threading.Tasks.Task class, 8

System.Threading.Tasks.TaskContinuatio nOptions enumeration, 115

System.Threading.Tasks.TaskCreationOpti ons enumeration, 15, 128

System.Threading.Tasks.TaskFactory class, 117

System.Threading.Tasks.TaskScheduler class, 156

System.Threading.Tasks.TaskScheduler.U nobservedTaskException member, 41

System.Threading.Tasks.TaskStatus enumeration, 43

System.Threading.Thread class, 157

System.Threading.ThreadLocal class, 55

System.Threading.WaitHandle class, 62, 72

System.Threading.WaitHandleCannotBeO penedException class, 76

Take(CancellationToken) method,

BlockingCollection class, 152

TakeFromAny( ) method, 155

TakeFromAny(BlockingCollection<T>[ ], out T, CancellationToken) method, 153

TakeFromAny(BlockingCollection<T>[ ], out T) method, 153

targetSum variable, 205

Task array, 30, 34

Task Programming Library (TPL), 1-2, 4, 7, 9, 15, 35

task scheduler, 9

Task<>.Factory.StartNew( ) method, 43

TaskContinuationOptions enumeration, 111, 113, 122, 127, 129

TaskContinuationOptions.AttachedToPare nt value, 129

Task.ContinueWith( ) method, 111, 113, 115,120, 160

Task.ContinueWith<int>( ) method, 112

Task.ContinueWith<T>( ) method, 112

TaskCreationOptions enumeration, 127

TaskCreationOptions.LongRunning enumeration value, 157

Task.CurrentId property, 15

Task.Factory class, 14

TaskFactory class, 117

Task.Factory property, 117

Task.Factory.ContinueWhenAll( ) method, 111,118, 120, 160

Task.Factory.ContinueWhenAny( ) method, 111, 119-120

Task.Factory.StartNew( ) method, 8-10, 14, 160

Task.Factory.StartNew<T>( ) method, 9 Task.IsCancelled property, 17 Task.Result property, 9 tasks, 7-48

cancelling, 15-25

creating composite cancellation token, 23

determining if task was cancelled, 24-25

monitoring by polling, 17

monitoring with delegate, 19-20

monitoring with wait handle, 20

multiple tasks, 22

child, creating, 126-129

common problems assuming status on any-to-one continuations, 164

deadlocked task scheduler, 169-172

excessive spinning, 47-48

inconsistent/unchecked cancellation, 162

local variable evaluation, 46

reusing objects in producers, 166

task dependency deadlock, 45

trying to take concurrently, 165

using BlockingCollection as IEnumerable, 168

continuations, 110-126

any-to-one, 117-119

canceling, 120-122

handling exceptions, 122-126

many-to-one, 117-119

one-to-many, 113

selective, 115

simple, 111-113

waiting for, 122

creating and starting, 8-15

creating simple, 9-10

getting result, 13-14

identifying, 15

setting task state, 11-13

specifying task creation options, 15

custom task scheduler creating, 156-160

using, 160

exception handling, 35-43

basic exceptions, 36-37

reading task properties, 39-41

using custom escalation policy, 4143

using iterative handler, 37-39 executing lazily, 43-44 Hello task, 7-8

Producer/Consumer pattern, 146-155

combining multiple collections, 151-155

creating pattern, 147-151

status of, 43

using synchronization to coordinate, 129-146

AutoResetEvent class, 141-142

Barrier class, 131-135

CountDownEvent class, 136-139

ManualResetEventSlim class, 139

SemaphoreSlim class, 143-146

waiting for, 25-34

one of many tasks, 34

several tasks, 33-34

single task, 31-32

using cancellation token wait handle, 26-27

using classic sleep, 27-28

using spin waiting, 29-30

TaskScheduler class, 41, 113, 156, 159-160, 181

Task.Start( ) method, 113, 160 Task.Status property, 43, 162

TaskStatus.WaitingForChildrenToComplet e value, 129

Task.WaitForAll( ) method, 30

TAType antecedent result, 119

TCType continuation result, 119

TerminalMethodA( ) method, 261

TerminalMethodB( ) method, 261

testing parallel performance making simple performance comparisons, 253-256

performing parallel analysis with Visual Studio, 256-260

using good coding strategies, 252-253

Thread Assignment column, 264

Thread class, 157-158, 160

thread local storage (TLS), 55, 57-59, 190192

ThreadLocal class, 55, 57

ThreadLocal<int> class, 57

Threads button, 258

Thread.SleepWait( ) method, 26

Thread.SpinWait( ) method, 29-30, 47

ThrowIfCancellationRequested( ) method, 16-17

TLS (thread local storage), 55, 57-59, 190192

ToArray( ) extension method, 233 ToArray( ) method, 160 ToDictionary( ) extension method, 233 Token property, 16, 26

Token.Register( ) method, 17

Token.ThrowIfCancellationRequested( ) method, 16

Token.WaitHandle.WaitOne( ) method, 17

ToList( ) extension method, 233

TPL (Task Programming Library), 1-2, 4, 7, 9, 15, 35

TryAdd( ) method, 151

TryAddCount( ) method, 136-137

TryAddCount(int) method,

CountDownEvent class, 137

TryAdd(T, int, CancellationToken)

method, BlockingCollection class, 150

TryAdd(T, int) method, BlockingCollection class, 150

TryAdd(T, TimeSpan) method,

BlockingCollection class, 150

TryAdd(T) method, BlockingCollection class, 150

TryAdd(TKey, TVal) method,

ConcurrentDictionary class, 95

TryAddToAny(BlockingCollection<T>[ ], T, int, CancellationToken) method,

TryAddToAny(BlockingCollection<T>[ ], T, int) method, 153

TryAddToAny(BlockingCollection<T>[ ], T) method, 153

TryAddToAny(BlockingCollection<T>[ ], T, Timespan) method, 153

try...catch...finally sequence, 105

TryDequeue( ) method, 89-90, 156

TryDequeue(out T) method,

ConcurrentQueue class, 91

TryEnter( ) method, 70

TryExecuteTask( ) method, 156-157, 160

TryExecuteTaskInLine( )method, 156, 159

TryGetValue( ) method, 95

TryGetValue(TKey, out TVal) method, ConcurrentDictionary class, 95

TryPeek(out T) method ConcurrentBag class, 94 ConcurrentQueue class, 91 ConcurrentStack class, 92

TryPop( ) method, 91

TryPop(out T) method, ConcurrentStack class, 92

TryPopRange( ) method, 91

TryPopRange(out T[ ], int, int) method, ConcurrentStack class, 92

TryPopRange(out T[ ]) method,

ConcurrentStack class, 92

TryRemove(TKey, out TVal) method,

ConcurrentDictionary class, 95

TryTakeFromAny(BlockingCollection<T>[ ], out T, CancellationToken) method, 153

TryTakeFromAny(BlockingCollection<T>[ ], out T, int) method, 153

TryTakeFromAny(BlockingCollection<T>[ ], out T) method, 153

TryTakeFromAny(BlockingCollection<T>[ ], out T, Timespam) method, 153

TryTake(out T, int, CancellationToken)

method, BlockingCollection class, 152

TryTake(out T, int) method,

BlockingCollection class, 152

TryTake(out T) method,

BlockingCollection class, 152

TryTake(out T) method, ConcurrentBag class, 94

Download from Wow! eBook

TryTake(out T, TimeSpan) method, BlockingCollection class, 152

TryUpdate(TKey, TVal, TVal) method, ConcurrentDictionary class, 95

0 0

Post a comment