Asynchronous and synchronous mechanisms in C ++

Source: Internet
Author: User
What does asynchronous and synchronous mean? At the beginning, I was puzzled by some questions. Later I summarized the information and figured out how to share it with you:

 
Asynchronous:After a request is initiated, do other things first without waiting for the response of the request to be initiated to return any response. Of course, do you wait until the response is returned? The key is to check whether the result is actually returned. If the result is returned, it is accepted. If the result is not returned, it will not wait until the end of the main function, the operating system will end and clear all the resources and traces of the process.

 
Synchronization:Compared with Asynchronization, it is necessary to wait until the response is returned after the request is initiated, and then do something else after the request is sent. This method seems very loyal.
 
 
For example, synchronous and asynchronous calls:

 
For example, yourProgramA method is called. It takes a long time to execute this method, and the time is not certain. The traditional method is "synchronous call", as shown below: Private int func () {// This method takes a long time and returns an int value .} Private void a () {int n = func (); textbox1.text = n. tostring (); // The N obtained here is the result of execution by func and displayed in textbox1 .} The above method can be called synchronous call. Obviously, it has a disadvantage: the execution of the func method is too long, the program will be blocked, and otherCodeThe user experience is that the entire program will be frozen. When the Task Manager is opened, the system will prompt that the program has no response. If the user thinks the program is dead, the program will be manually ended, asynchronous calls are different. after calling the func method, you do not have to wait for func to complete execution. You can execute other code until func finishes execution and the result is returned, if func provides the Asynchronous Method funcasync (), you can call: Private void a () {funcasync (); // The funcasync method is executed here and will not be blocked, after the program executes the funcasync method in the background, it automatically calls the funccompleted method and passes the result .} Private void funccompleted (int n) {textbox1.text = n. tostring (); // parameter n is the result returned by asynchronous callback ,}

 

Source: from Baidu

 
 

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.