Differences between Asynchronization and multithreading in C #

Source: Internet
Author: User
  • Differences between Asynchronization and multithreading in C #
  • Http://developer.51cto.com Ruyi blog park I want to comment (0)
    What is the difference between Asynchronization and multithreading? What are the characteristics of asynchronous and multithreading? This article will introduce the differences between asynchronous and multithreading and the differences between asynchronous 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.

    Differences between asynchronous and multithreading: the 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.

    Difference between Asynchronization and multithreading: 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.

    Differences between asynchronous and multithreading 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. It is highly complex to write asynchronous operations. The program mainly uses the callback method for processing, which is a bit difficult to debug with the way of thinking of ordinary people.

    Differences between Asynchronization and multithreading 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, but it is not suitable if you need to handle a large number of concurrent operations.

    Case study of differences between asynchronous and Multithreading

    Some brothers may have been impatient with the theory. Now let's look at several practical asynchronous operation examples.

    Asynchronous and multithreading: What is the difference between the asynchronous method generated by Delegate?

    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? Let's use a piece of code to prove it.

 
 
  1. UsingSystem;
  2. UsingSystem. Threading;
  3.  
  4. NamespaceAsyncdelegatedemo
  5. {
  6. Delegate VoidAsyncfoo (IntI );
  7. ClassProgram
  8. {
  9. /// <Summary>
  10. /// Output the information of the current thread
  11. /// </Summary>
  12. /// <Param name = "name"> method name </param>
  13.  
  14. Static VoidPrintcurrthreadinfo (StringName)
  15. {
  16. Console. writeline ("thread ID of" + name + "is :"
  17. + Thread. currentthread. managedthreadid + ", current thread is"
  18. + (Thread. currentthread. isthreadpoolthread? "": "Not ")
  19. + "Thread Pool thread .");
  20. }
  21.  
  22. /// <Summary>
  23. /// Test method, sleep for a certain period of time
  24. /// </Summary>
  25. /// <Param name = "I"> sleep time </param>
  26. Static VoidFoo (IntI)
  27. {
  28. Printcurrthreadinfo ("Foo ()");
  29. Thread. Sleep (I );
  30. }
  31.  
  32. /// <Summary>
  33. /// Deliver an asynchronous call
  34. /// </Summary>
  35. Static VoidPostasync ()
  36. {
  37. Asyncfoo caller =NewAsyncfoo (FOO );
  38. Caller. begininvoke( 1000,
  39. NewAsynccallback (foocallback), caller );
  40. }
  41.  
  42. Static VoidMain (String[] ARGs)
  43. {
  44. Printcurrthreadinfo ("Main ()");
  45. For(IntI = 0; I <10; I ++)
  46. {
  47. Postasync ();
  48. }
  49. Console. Readline ();
  50. }
  51.  
  52. Static VoidFoocallback (iasyncresult AR)
  53. {
  54. Printcurrthreadinfo ("foocallback ()");
  55. Asyncfoo caller = (asyncfoo) Ar. asyncstate;
  56. Caller. endinvoke (AR );
  57. }
  58. }
  59. }

Differences between asynchronous and multithreading the output of the Instance code is as follows:

 
 
  1. Thread Id of Main() is: 1,   
  2. current thread is not thread pool thread.  
  3.  
  4. Thread Id of Foo() is: 3,   
  5. current thread is thread pool thread.  
  6.  
  7. Thread Id of FooCallBack() is: 3,   
  8. current thread is thread pool thread.  
  9.  
  10. Thread Id of Foo() is: 3,   
  11. current thread is thread pool thread.  
  12.  
  13. Thread Id of Foo() is: 4,   
  14. current thread is thread pool thread.  
  15.  
  16. Thread Id of Foo() is: 5,   
  17. current thread is thread pool thread.  
  18.  
  19. Thread Id of FooCallBack() is: 3,  
  20.  current thread is thread pool thread.  
  21.  
  22. Thread Id of Foo() is: 3, 

The basic analysis content of the differences between asynchronous and multithreading will be introduced here, hoping to help you understand and learn the differences between asynchronous and multithreading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.