What is asynchronous programming
Let’s talk about what is synchronous and asynchronous in an easy-to-understand way. I believe there will be no more doubts after reading.
1. Synchronous blocking
One day you are hungry and want to eat, but you can’t do it. Then you say to your mother: Mom, I am hungry. I want to eat. And then your mom went to the kitchen to cook,You just sit outside and wait for the meal to be readyThis is synchronous blocking. (The caller blocking can’t do anything else)
2. Synchronization is non-blocking
Similarly, one day you are hungry and want to eat, but you can’t do it. Then you say to your mother, “Mom, I am hungry. I want to eat.” And then your mom went to the kitchen to cook,And then you watch TV and ask your mom every once in a while, is dinner ready. This is synchronous nonblocking. (Instead of blocking, the caller keeps polling to check the state)
3. Asynchronous mode 1
Similarly, one day you are hungry and want to eat, but you can’t do it. Then you say to your mother, “Mom, I am hungry. I want to eat.” Then your mom went to the kitchen to cook, and then you went to watch TV. When your mom cooks dinner, Bao, it’s ready,Go to the kitchen and put the food on the table. (The caller does nothing and waits to get the result)
4. Asynchronous mode 2
Similarly, one day you are hungry and want to eat, but you can’t do it. Then you say to your mother, “Mom, I am hungry. I want to eat.” Then your mom went to the kitchen to cook, and then you went to watch TV. When your mother makes dinner, she says, “Bao, dinner is ready. Come and eat.”Just go straight to the table and eat. (The caller does not care, the result is already in place, just look at the result)
Asynchronous 2 is definitely better than asynchronous 1, but the Linux kernel does not support it, so you have to use asynchronous 1.
One final point: only synchronization can block and non-block. Asynchrony is non-blocking. Think about it.