Asynchronous programming, c # asynchronous programming
Concept
Before speaking about Asynchronization, let's talk about these concepts: Asynchronization/synchronization and multithreading.
Asynchronous: Indicates the operation after another operation is called without waiting for the result. If no other operation is performed afterwards, the current thread enters the sleep state, the CPU time will have the opportunity to switch to other threads. After the asynchronous operation is complete, you can use the callback function to obtain notifications and results. There are multiple asynchronous implementation methods, such as multithreading and Completion Ports. Multithreading puts asynchronous operations into another thread for operation, and receives a notification through polling or callback. The port is completed, and the operating system takes over Asynchronous Operation Scheduling and hardware interruption, the callback method is triggered upon completion. This method does not require additional threads.
Synchronization: Corresponds to Asynchronization.
Multithreading: Multiple threads (including their data structures, contexts, and code snippets) in a process run collaboratively. Multiple Threads in a multi-core computer have the opportunity to run on multiple cores at the same time. If computing is performed in a thread, parallel computing is performed.
Asynchronous multithreading in. NET
As mentioned in the concept, multiple threads can be used to implement asynchronous operations. Refer to the multithreading in. NET later.
Classic Async Pattern (CAP) (BeginXXX & EndXXX)
Many classes such as delegate and FileStream contain asynchronous interfaces of ininxxx & EndXXX, which can be used to implement asynchronous programming. However, whether multithreading or port completion is used in this model depends on the specific implementation. The delegated (BeginInvoke & EndInvoke) is implemented based on multithreading. FileStream also needs to pass in FileOptions. Asynchronous to inform you in advance that it will use Asynchronous IO reading. Otherwise, the default multi-thread implementation will be used.
Event-based Async Pattern (EAP) (XXXAsync & XXXComplete)
In addition to the preceding programming mode, the. NET Framework also provides event-based asynchronous programming mode. For example, many WebClient methods provide asynchronous versions, such as DownloadString.
Synchronization version:
Public string DownloadString (string url );
Asynchronous version:
Public void DownloadStringAsync (string url );
Public event DownloadStringCompleteEventHandler DownloadStringComplete;
Task-based Asynchronous Pattern (TAP) (async & await)
Both the above two asynchronous programming models (CAP & EAP) divide asynchronous implementation into two parts: one is to initiate asynchronous operations and the other is to obtain results. If multiple asynchronous calls need to be executed sequentially, the Code becomes very messy, resulting in nesting of another asynchronous call in one asynchronous callback.
And TAP is used to solve this callback disaster. Two new language keywords are introduced to help users write asynchronous programming code (async & await ).
Multithreading Thread in. NET
The base class of the managed thread.
ThreadPool
Internally implemented thread pools are used cyclically to avoid frequent creation of threads and improve efficiency.
Task
Use the internal thread pool. More convenient thread creation, exception capture, and multi-task synchronization.
Exclusive thread lock
Monitor & lock
Mutex lock
Mutex can implement cross-process locks.
Read/write lock
ReaderWriterLock and Reader-Writer are two types of requests in the order of arrival:
Reader-Reader. The second Reader directly obtains read control without waiting;
Reader-Writer, the second must wait until the first call ReleaseReaderLock () to release the read control before obtaining the write control;
Writer-Writer. The second one needs to wait for the first call ReleaseWriterLock () to release write control before obtaining write control;
Writer-Reader. The second one needs to wait for the first call ReleaseWriterLock () to release write control before obtaining read control.