Today saw "WIN32 multithreading Programming" synchronization control, only to find that the original synchronization and asynchronous concept is very vague, even confused. So Google a bit.
The following are the views of the high people, simple and clear.
Synchronization means that after the sender sends out the data, the receiver sends back the response before sending the next packet to the communication mode.
Asynchronous means that after the sender sends out the data, the receiver sends back the response, and then the next packet is communicated.
To cite a less appropriate example, like this:
SendMessage (...)
TRACE0 ("Just like Send");
PostMessage (...)
TRACE0 ("Just like WSASend using overlapped");
SendMessage is called when the call does not return, and so on after the message response to execute TRACE0, which is synchronization.
PostMessage is returned immediately after the call and executes TRACE0 without a message response, which is asynchronous.
Answer three:
The difference between synchronous and asynchronous
For example: Ordinary B/s mode (synchronous) Ajax Technology (asynchronous)
Synchronization: Submit request, wait for server processing, processing completed returns this period the client browser cannot do anything
Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)
--------------------------------------------------------------------------------------------------------------- -----
Synchronization is when you ask me to eat, I hear you go to dinner, if not heard, you keep barking until I tell you to hear, to go to dinner together.
Async is you call me, and then go to dinner, I can get the news immediately, may wait until after work to eat.
So, if I ask you to eat in a synchronized way, ask me to eat with the Async method, so you can save money.
--------------------------------------------------------------------------------------------------------------- -----
For example, a synchronous message is asynchronous when you call
-------------------------------------------------------------
Concepts of synchronous, asynchronous, blocking, and non-blocking
In the network programming, we often see synchronous, asynchronous, blocking and non-blocking four ways to call. These ways are not well understood in each other's concepts. Here is my understanding of these terms.
Synchronous
The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. By this definition, the vast majority of functions are synchronous calls (such as sin, isdigit, etc.). But generally speaking, we are talking about synchronous and asynchronous tasks, especially those that require other components to collaborate or need to be done in a certain amount of time. The most common example is SendMessage. The function sends a message to a window that does not return until the other party finishes processing the message. When the other party finishes processing, the function returns the LRESULT value returned by the message handler function to the caller.
Asynchronous
Asynchronous concepts and synchronization are relative. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion. Take the Casycsocket class as an example (note that CSocket derives from CAsyncSocket, but the function has been converted from asynchronous to synchronous), and when a client makes a connection request by calling the Connect function, the caller thread can run down immediately. When the connection is actually set up, the bottom of the socket sends a message informing the object. It is mentioned here that the executing parts and callers return results in three ways: status, notifications, and callbacks. Which one can be used depends on the implementation of the execution part, which is not controlled by the caller unless the execution part provides multiple choices. If the execution part is notified by the state, then the caller needs to check every time, the efficiency is very low (some beginners of multithreaded programming, always like to use a loop to check the value of a variable, which is actually a very serious error). If you are using notifications, the efficiency is high because there is almost no extra action required to execute the part. As for the callback function, there is not much difference from the notification.
Blocking
A blocking call means that the current thread is suspended until the call results are returned. Functions are returned only after the result is obtained. Someone might equate blocking calls with synchronous calls, and in fact he is different. For synchronous calls, many times the current thread is still active, but logically the current function does not return. For example, we call the receive function in CSocket, and if there is no data in the buffer, the function waits until there is data to return. At this point, the current thread will continue to process a wide variety of messages. If the main window and the calling function are in the same thread, the main interface should be refreshed unless you call in a special interface action function. Another function that the socket receives data recv is an example of a blocking call. When the socket is working in blocking mode, if the function is called without data, the current thread is suspended until there is data.
Non-blocking
The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.
Blocking mode and blocking function calls for objects
Whether the object is in blocking mode and if the function is not a blocking call has a strong correlation, but not one by one corresponds. Blocking objects can have non-blocking calls, we can use a certain API to poll the state, the appropriate time to call the blocking function, you can avoid blocking. For non-blocking objects, calling a special function can also enter a blocking call. The function Select is an example of this.
Asynchronous Request:
Nsmutabledata*Buf= [[Nsmutabledata Alloc] Initwithlength:0];nsurlconnection* Connection = [[nsurlconnection Alloc] initwithrequest:ReqDelegate: Self];//When a response is received, it triggers - (void)Connection:(nsurlconnection *)AconnectionDidreceiveresponse:(Nsurlresponse *)Aresponse;//You can determine the return result in the inside, or handle the information in the HTTP headers returned//Each time a data is received, it is called once - (void)Connection:(nsurlconnection *)AconnDidreceivedata:(NSData *)Data;//So generally speaking, it is - (void)Connection:(nsurlconnection *)AconnDidreceivedata:(NSData *)Data { [BufAppendData:Data];} //Of course buffer is the same as the previous Initwithrequest declaration.//Network error triggered - (void)Connection:(nsurlconnection *)AconnDidfailwitherror:(Nserror *)Error;trigger when all data received is complete - (void)connectiondidfinishloading : (nsurlconnection *)aconn;
Sync Request:
Nsmutableurlrequest