Chapter II command-style data parallelism
Several parallel types:
Data parallelism
Task parallelism
Pipeline
Under the System.threading.tasks.parallel
Parallel.For
Parallel.ForEach (can be customized Parytitioner)
Parallel.Invoke
1. Parallel.Invoke
No specific execution order
Interleaved Concurrency: Logical cores implement parallel artifacts through time-slice mechanisms and fast context switches
Concurrency: Not explained
Hot spots: Potentially parallel code
Speedup: Serial execution time/parallel execution time
2. Parallel.For
Each iteration is assigned to a thread as a task
The upper bounds of the iteration range are less than, so it is necessary to add a
3. Parallel.ForEach
Can be partitioned through the partitioner partition, with each iteration processing a partition,tuple<int,int>
Exit
ParallelLoopState
Break: Does not end immediately, execution is less than the current iteration of the content
Stop: Exit as soon as possible
Parallelloopresult
IsCompleted Cycle Complete
! IsCompleted &&! Lowestbreakiteration.hasvalue Stop Termination
! IsCompleted && Lowestbreakiteration.hasvalue Break terminated
AggregateException
. InnerExceptions Internal Exception Collection
ParallelOptions
Maxdegreeoofparallelism degree of parallelism
CancellationToken Cancel Token
TaskScheduler Task Scheduler
Chapter III Command-parallel task parallelism
System.Threading.Tasks.Task
A task represents an asynchronous operation and does not target a thread
State
Taskstatus.created Initial State
. Waitingforactivation the initial state of tasks that depend on other tasks
. Waitingtorun the initial state of a task created by taskfactory.startnew
. Cancelled
. Faulted
. Runtocompletion
Task. Start ()
Task. Wait () Waits also have overloads that are timed out
Task.waitall ()
The Task.TaskFactory.StartNew method can pass arguments to the CancellationToken, or taskcreationoptions
Taskcreationoption
. AttachedToParent associated with a parent task
. None default behavior
. Longrunning long-time operation, the scheduler can be coarse-grained operation, generally more than one second
. Preferfairness tells the scheduler that earlier scheduled tasks are executed earlier
Task. ContinueWith Continuation Tasks
Taskcontinuationoptions operation or condition
C # Parallel programming