Document directory
- Nature of asynchronous operations
- Thread nature
- Advantages and disadvantages of asynchronous operations
- Advantages and disadvantages of Multithreading
- Asynchronous example
- References:
I have been very busy recently, so I am busy writing my blog with time. I have inherited the previous style and continue to talk about Hu Kan.
Recently, I encountered the problem of socket Asynchronous Network Transmission in the project. Then, I first asked my mother, and found an article titled differences between Asynchronization and multithreading in C # (See document 1, I personally think I understand it very well. After learning it myself, I processed it again to share it with you. I hope you will have a clear understanding of Asynchronization and multithreading.
What is the difference between asynchronous and multithreading in C? Both asynchronous and multithreading can avoid calling thread blocking and improve software responsiveness. Sometimes we think that Asynchronization and multithreading are equivalent. However, there are some differences between asynchronous and multithreading. These differences lead to the difference in the time to use Asynchronous and multithreading.
Nature of asynchronous operations
All programs will eventually be executed by computer hardware. Therefore, to better understand the nature of asynchronous operations, we need to understand its hardware base. Friends who are familiar with computer hardware must be familiar with the DMA term. The technical specifications of hard disks and optical drives all have clear DMA mode indicators. In fact, NICS, sound cards, and video cards also have DMA functions. DMA means direct access to memory. That is to say, hardware with DMA functions can exchange data with memory without consuming CPU resources. As long as the CPU sends a command when initiating data transmission, the hardware starts to exchange data with the memory. After the transmission is complete, the hardware triggers an interruption to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis of asynchronous operations. Therefore, asynchronous DMA operations can be initiated even in a single process (and wireless process) system such as DOS.
Thread nature
A thread is not a computer hardware function, but a logic function provided by the operating system. A thread is essentially a piece of code that runs concurrently in a process, therefore, threads require the operating system to invest CPU resources for running and scheduling.
Advantages and disadvantages of asynchronous operations
Because asynchronous operations do not require additional thread burden and Use callback for processing, the processing function does not need to use shared variables (even if it cannot be completely used, at least the number of shared variables can be reduced) to reduce the possibility of deadlocks. Of course, asynchronous operations are not perfect. The complexity of asynchronous operations is relatively high. The program mainly uses the callback method for processing, which is somewhat different from the thinking method of ordinary people and is difficult to debug.
Advantages and disadvantages of Multithreading
The advantages of multithreading are obvious. The processing program in the thread is still executed in sequence, which conforms to the thinking habits of ordinary people, So programming is simple. However, the disadvantages of multithreading are also obvious. The use (abuse) of threads will impose additional burden on context switching. In addition, shared variables between threads may cause deadlocks.
Applicability
After understanding the advantages and disadvantages of threads and asynchronous operations, we can discuss the rational use of threads and asynchronous operations. In my opinion, when I/O operations are required, it is more appropriate to use asynchronous operations than to use thread + synchronous I/O operations. I/O operations include not only direct file and network read/write operations, but also cross-process calls such as database operations, Web Service, httprequest, And. Net remoting.
The applicability of threads is those that require long CPU operations, such as time-consuming graphics processing and Algorithm Execution. However, due to the simplicity and habit of using thread programming, many friends often use threads to execute time-consuming I/O operations. In this way, there are only a few concurrent operations, which is not suitable if you need to handle a large number of concurrent operations.
Asynchronous example
As you may know, using delegate can "automatically" enable asynchronous calls to a method. Intuitively, I think the compiler or CLR uses another thread to execute the target method. Is that true?
Using system; using system. threading; namespace asynchronous {delegate void asyncfoo (int I ); class program {// <summary> // output the information of the current thread // </Summary> // <Param name = "name"> method name </Param> static void printcurrthreadinfo (string name) {console. writeline ("thread ID of" + name + "is:" + thread. currentthread. managedthreadid + ", current thread is" + (thread. currentthread. isthreadpoolthread? "": "Not") + "Thread Pool thread. ");} // <summary> // test method, sleep for a certain time // </Summary> // <Param name = "I"> sleep time </param> static void Foo (int I) {printcurrthreadinfo ("Foo ()"); thread. sleep (I);} // <summary> // post an asynchronous call // </Summary> static void postasync () {asyncfoo caller = new asyncfoo (FOO); caller. begininvoke (1000, new asynccallback (foocallback), caller);} static void main (string [] ARGs) {printcurrthreadinfo ("Main ()"); For (INT I = 0; I <5; I ++) {postasync ();} console. readline ();} static void foocallback (iasyncresult AR) {printcurrthreadinfo ("foocallback ()"); asyncfoo caller = (asyncfoo) ar. asyncstate; caller. endinvoke (AR );}}}
The program output is as follows:
It seems that the answer is yes.
This example program is very good. It can be said that it is an asynchronous standard template. The meaning is not enough.
References:
Differences between asynchronous and multithreading in C #
C # network socket programming (basic concepts and operations)-part.1, 2, 3, 4, 5
Digress:
After nearly three weeks (16 days) of hard work, this morning, the company finally upgraded an existing project to asynchronous mode, and the hospital passed the on-site test! Two pieces are involved, one is the outpatient doctor Station Program at the client side, and the other is the server side! The latter upgrade may not improve the competitiveness of debuglzq's entrepreneurial company because different systems can share this server!
Once again, debuglzq has realized that the lab's products are commercially available, and there are a series of troubles to go through-network transmission speed and (large) data concurrency, user experience, database system reliability, program stability (abnormal recovery), software system security, and sometimes some software and hardware environments need to be considered. It's not easy to come along with the "boss! Only debuglzq knows this taste. Maybe debuglzq is about to leave the current company soon, because it is about to graduate soon. I still hope that the teachers and sisters can pass on the technology and that the "boss" company will thrive and grow stronger and bigger!