Novice learning parallel programming, refer to the "C # Parallel Programming advanced Tutorial." PDF, if you have any errors, please correct me.
Website: https://www.cnblogs.com/woxpp/p/3928788.html
Directory
C # Parallel programming-related concepts
C # Parallel Programming-parallel
C # Parallel Programming-task
C # Parallel Programming-Concurrent collections
C # Parallel Programming-thread synchronization Primitives
C # Parallel Programming-plinq: declarative data parallelism
Mission Brief
TPL introduces a new task-based programming model that enables multi-core efficiency and performance of the application without the need to write the underlying complex and heavyweight thread code.
However, it is important to note that the task is not a thread (a thread is required when the task is running, but it does not mean that the task replaces the thread, that the task code is run using the underlying thread (software threads, scheduling on a particular hardware thread or logical core) and that there is no one-to-one relationship between the task and the thread. )
When a new task is created, the scheduler (the scheduler relies on the underlying thread pool engine) uses a work-stealing queue to find the most appropriate thread, and then joins the task into the queue, and the code that the task contains runs in a thread.
System.Threading.Tasks.Task
A task represents an asynchronous operation, and the task provides many methods and properties that enable it to control the execution of a task and to obtain its state information.
Task creation and execution are independent, so you can have complete control over the execution of the associated operation.
Using parallel execution of Parallel.For, Parallel.ForEach Loop iterations, the TPL creates an instance of System.Threading.Tasks.Task in the background.
When using Parallel.Invoke, the TPL also creates an instance of System.Threading.Tasks.Task that is consistent with the number of delegates invoked.
Note Item
When a lot of asynchronous operations are added as task instances, the Task Scheduler tries to run these tasks in parallel and tries to load balance all available cores to make full use of all the available logical cores at runtime.
However, in the actual coding process, not all code fragments can be easily run with the task, because the task brings additional overhead, although this overhead is less than the cost of adding threads, but still need to take this overhead into account.
Task State and life cycle
A task instance will only complete its life cycle once, and when the task reaches its final state of 3 Ken, it cannot go back to any previous state
The following code, detailed in the comments, easy to understand the status of the task:
View Code
Using Tasks to Parallelize code
Multiple methods can be loaded in parallel using Parallel.Invoke, and the same work can be done with a task instance, with the following code:
View Code
Wait for the task to complete Task.waitall
Task.waitall method, this method is executed synchronously, the main thread does not continue to execute the next instruction until the task is accepted as a parameter, and all tasks end its execution, the following code is pasted
View Code
Task.waitall Limit Wait Duration
View Code
10 milliseconds without completing the task, the output is
Cancel a task by canceling a tag
The execution of a task instance is interrupted by a cancellation token. Cancellationtokensource,cancellationtoken The Iscanceled property flag is currently canceled, cancel the task, the task does not necessarily cancel immediately, the following code:
View Code
Task exception Handling when many tasks run in parallel, many exceptions can occur in parallel. A task instance can handle a set of exceptions that have system.aggregateexception class handling
View Code
Task return value Task<tresult>
View Code
Concatenation of multiple tasks by continuation
ContinueWith: When a target task is created, an asynchronous execution of the continuation program, await, as shown in the code:
View Code
Taskcontinuationoptions
The Taskcontinuationoptions parameter allows you to control the optional behavior that extends the scheduling and execution of another task. Here's a look at the code:
View Code
The Taskcontinuationoptions property has many, as shown below
C # Parallel Programming-task