❝
Abstract: Better understand the concepts and mechanisms of synchronous/asynchronous, blocking/non-blocking.
❞
I. Synchronous and asynchronous
Synchronous/asynchronous, which are notification mechanisms for messages.
1. Concept explanation
synchronous
❝
Synchronization means that when a function call is made, it does not return until the result is received.
❞
The most common example is SendMessage. This function sends a message to a window and does not return until the window has processed the message. The function returns the value returned by the message handler to the caller only after the other party has finished processing.
Most of our common method calls are synchronous, calling another method inside the method, often waiting for the method to process the result and then get the result, and then carry on the subsequent processing.
When we do breakpoint debugging, we can see that the method execution is like a stream, executing one sentence at a time, waiting for the execution to complete.
This idea can be better understood by looking at processes that require collaboration or take a certain amount of time.
“For example, if a file is fetched in a synchronization mechanism, the subsequent code cannot be executed until the contents of the file are returned.“
asynchronous
❝
Asynchrony is the opposite of synchronization.
❞
When an asynchronous procedure call is made, the caller does not get the result immediately. The widget that actually handles the call notifies the caller after the call has been made, either through status, notifications, or through callback functions.
In the Socket example, when a client makes a connection request by calling Connect, the caller thread can continue running immediately without waiting for the result. When the connection is actually established, the socket layer sends a message to notify the object.
The executor and caller can return results in three ways:
- state
- notice
- Callback function.
Which one can be used depends on the implementation of the executor and is not controlled by the caller unless the executor provides multiple choices.
- If the executer uses state notification, then the caller needs to check it every once in a while, which is very inefficient. Some people who are new to multithreaded programming, always like to use a loop to check the value of a variable, which is actually a very serious mistake.
- If notifications are used, the efficiency is high because the executing part does little extra work.
- As for callback functions, they are not much different from notifications.
In my understanding of state, notification and callback, “state” means that the caller and the executor negotiate a state. The executor changes the public signal when executing different states. The caller keeps checking whether the state has changed and then performs the corresponding things according to the change. Notice is when I’m done and I tell you to do something. The “callback” is similar.
2. Give examples
In 1940, AS a senior party agent, I was dispatched by the organization to go deep behind enemy lines and carry out undercover operations.
Synchronization: The organization does nothing until it gets my results, waits for my results, and then takes action.
“Asynchronous” : The ability of the organization to do things that do not depend on my results, such as cutting off a path or taking a position:
❝
Status: I agreed with the organization, with the gate building flag as a signal, white flag, devil to sweep up, yellow flag, devil out of the city, the organization arranged for individuals to see the flag every day, ah, efficiency certainly not.
❞
❝
Notice: the devil out, I took out the iPhone, directly call my superior leadership, they will be ready to play.
❞
❝
Callback: The organization is already ready, I’m done, go straight to the things that are ready.
❞
In asynchronous messaging notification mechanism, waiting for the news (in this case is wait for to deal with business people) tend to register a callback mechanism, are waiting for the event is triggered by the triggering mechanism (in this case, the counter) a mechanism (here is written in the book of the little note number) find people waiting for the event.
In IO multiplexing mechanisms such as SELECT /poll, this is fd. When a message is fired, the trigger mechanism uses FD to find a handler for that FD.
3. In the actual program
Synchronous message notification mechanism
❝
Like simple read/write operations, they wait for both operations to succeed before returning;
❞
❝
Synchronization, in which the message handler waits for the message to be triggered.
❞
Asynchronous message notification mechanism
❝
Multiplexing IO operations, such as SELECT /poll, are notified by a message triggering mechanism to trigger the processing of messages when the message of interest is triggered.
❞
❝
Asynchronous, where a trigger mechanism notifies the handler of the message.
❞
Or back to the example above, devil to turn hadn’t come, this is your attention, and how do we deal with, is for the processing of this message, there is a difference between the two, but in the real IO operations: message of concern is: whether the fd read-write, and handling of the message is: to read and write the fd.
Synchronous/asynchronous are only concerned with how the message is notified, they are not concerned with how the message is handled, for example, the person at the bank merely notifies you that it is your turn to conduct a transaction, and they do not know which transaction to conduct (deposit or withdraw money).
Blocking and non-blocking
Blocking/non-blocking, which is the state of the program while it waits for messages, whether synchronous or asynchronous.
1. Concept explanation
A, blocking
A blocking call means that the current thread is suspended until the result of the call is returned. The function returns only after the result is obtained. One might equate blocking calls with synchronous calls, but they are different. For synchronous calls, many times the current thread is still active, but logically the current function does not return.
The socket receiving data function recv is an example of a blocking call. If the socket is in blocking mode and no data is available, the current thread is suspended until data is available.
B. Non-blocking
Nonblocking, as opposed to blocking, means that the function does not block the current thread but returns immediately until the result is not immediately available.
C, object blocking mode and blocking function calls
There is a strong correlation between whether an object is in blocking mode and whether a function is blocking, but there is not a one-to-one correspondence. There can be non-blocking calls on blocking objects. We can poll the state through certain APIS and call blocking functions when appropriate to avoid blocking. For non-blocking objects, calling a special function can also enter a blocking call. The function SELECT is one such example.
2. Give examples
Continuing with the example above, whether it’s a person sitting around waiting for a message or a call waiting for a notification, the mechanism is blocking if the waiting person can do nothing else but wait for the message during the wait.
Performance in the program, the program has been blocked in this place can not continue to perform the function call, on the contrary, we can slow a gun, when waiting for buried mine, and such a state is blocked, because he (waiting) no block on this news, but wait while doing their own thing.
Three, easy to confuse the point
Many people also confuse asynchronous with non-blocking because asynchronous operations are usually not blocked at real IO operations. For example, if you use select, reading when select returns readable will not be blocked.
As you can see, synchronous/asynchronous and blocking/non-blocking are two different concepts that can be combined together. Many people confuse synchronous and blocking because they don’t distinguish between the two concepts. For example, the blocking read/write operation combines message notification with processing messages. The message in question is whether the FD is readable/writable, while the processing message is read/write to the FD. When we set the FD to non-blocking, the read/write operation will not block waiting for message notification. If the FD is not readable/writable, the operation will return immediately.
4. Combination analysis of synchronous/asynchronous and blocking/non-blocking
“Synchronous blocking form:“
The least productive, in the example above, is when you wait and do nothing else. The actual program does not set the O_NONBLOCK flag bit to read/write operations on fd.
“Asynchronous blocking form:“
An asynchronous operation can be blocked, but not while processing a message, but while waiting for a message to be fired. For example, if the last timeout argument passed to the select function is NULL, then if none of the events in question are fired, The program will always block at this select call.
“Synchronous non-blocking form:“
In fact, it is inefficient. Imagine that you are doing something while checking to see if the message is coming. If sharpening the gun and observing the message were two operations of the program, the program would have to switch back and forth between these two actions. Many people write blocking read/write operations, but remember to set the O_NONBLOCK flag bit to the fd to make the synchronous operation non-blocking.
This article is formatted using MDNICE