Understanding of "synchronous" operations in asynchronous programming

1. What is synchronization?

It is not easy to make "synchronization" clear. Because in life, this word has many meanings, and you can have different understandings in different scenarios.

When you are playing with your mobile phone, watching TV, and eating fruit, "synchronization" can be understood as: doing many things at the same time, these things are "synchronized".

When you are facing the computer and adjusting the watch time, "synchronization" can be understood as: keeping a certain state of two things consistent. For example, you can synchronize the time on the watch with the time on the computer.

The meaning of "synchronization" in the computer field is different from the meaning of "synchronization" in life.
In the computer field, the "same" of "synchronization" does not mean "simultaneous", this is very important! In the computer field, "synchronization" focuses on coordination .

Coordination is also related to "mutual exclusion operations". "Mutual exclusion operation" means that two operations cannot be performed at the same time. For example, you cannot play with your mobile phone while you are asleep, you must coordinate the time of playing with your mobile phone and sleeping.

Since there is "mutual exclusion", then, in an event, certain operations with dependencies must have the order of "who comes first, who comes after".

Now, imagine this scenario: on weekends, you rest at home. On weekdays, working hours are tight and work is heavy, so I don’t have a good meal. Have a free time today, you want to cook a delicious meal, and reward yourself.

Cooking is a thing, with its own steps and operations. Let's divide it into three steps for now!

1. 准备食材
2. 烹饪食材
3. 享用美味

We can see that the operations in each of these three steps are "mutually exclusive". We cannot cook it while preparing the ingredients, nor can we enjoy it during the cooking process.

Moreover, there is a sequential dependency between the three steps. We must prepare ingredients before cooking, and enjoy delicious dishes after cooking. (Eat it raw will cause diarrhea...)

The order of execution of the three steps must be like this:

准备食材
烹饪食材
享用美味

In the computer field, each module operation in the code is executed in a certain logical sequence, and an operation must wait for the execution of the last operation it depends on to finish before it can start execution. Such a process is called "synchronization".

Synchronization focuses on the coordination and consistency of the entire code execution process . For example, you must enter the correct QQ number and password before you can use it to send and receive messages.

In short, synchronization means ordering, and waiting

END.

Search on WeChat, follow the official account: a programmer's day

Guess you like

Origin blog.csdn.net/pythontide/article/details/108364615