directory

  • Synchronous and asynchronous
  • Concurrency and parallelism
    • Concurrent Concurrency
    • Parallel Parallelism
    • Diagram concurrency and parallelism
  • Processes, threads, and coroutines
    • Process the Process
    • Thread the Thread
    • Process and thread + single-core and multi-core
    • Coroutines Coroutine

I believe that thread, process, coroutine, concurrency, parallelism, synchronization, asynchrony these concepts we will certainly encounter in the programming process, but it happened that these concepts are so similar, it is easy to blur, today I will use this article to comb through these concepts with you.

1. Synchronous and asynchronous

  • When we perform a task synchronously, we need to wait until the task is finished and give us feedback before we can perform other tasks.
  • But when performing one task asynchronously, we can perform another task without waiting for that task to finish. This is like the asynchronous refresh operation commonly used. A callback function can be used to retrieve the result of a task executed asynchronously.

We can clearly see the difference between synchronous and asynchronous operations, so asynchronous operations are so good why not use asynchronous operations? The meaning of the existence of synchronous operation is that some tasks must be carried out in order, as we go to the bank to take money, at least to ensure that you can take money in the bank account, or is not to rob the bank 🌝.

2. Concurrency and parallelism

2.1 concurrent Concurrency

We used to make breakfast for example, for example, say you haven’t wake up, your girlfriend has got up early, in the kitchen preparing breakfast for you, she want to give you do a slice of bread and Fried eggs, her as soon as possible in order to let you eat breakfast, opened two POTS, a pot to fry eggs, a pan used for frying bread, but she to food as delicious as possible, can only look at the pan Fried eggs, And then you go and look at the toast, and you look at each pan for a minute, and you switch back and forth, and you finally get the toast and the omelet, and you wake up and you have a delicious breakfast.

Your girlfriend’s breakfast process is a simultaneous one, because she can’t focus on two pans at the same time and can only do it by gradually switching her attention. But if your girlfriend can switch between the two POTS fast enough 😂, it will look as if she is operating both POTS at the same time 🤣.

2.2 the parallel Parallelism

Let’s take making breakfast as an example. Now you and your girlfriend have been together for a long time, and she can’t stand cooking for you every morning anymore, so this morning she dragged you up to cook with her. At this time, you and your girlfriend share a pot, you are preparing to fry eggs, your girlfriend is frying bread, you two do their own things in harmony without interfering with each other, and soon have a delicious breakfast.

You and your girlfriend cook at the same time, and the way each person handles a pot is parallel operation.

Ps: will you suddenly think of, the original oneself have no girlfriend 🙄.

2.3 Diagram concurrency and parallelism

Suppose we have two tasks A and B, and we use concurrent execution like this.

Let’s look at the parallel approach.

3. Processes, threads and coroutines

3.1 Process Process

The startup of each piece of software on the computer represents a process that loads a program written into the operating system to perform a predetermined task. The operating system assigns resources to processes to support them to complete tasks, and each process is assigned a unique PID.

3.2 the Thread Thread

Thread is the execution instance of the process, is the smallest unit of the execution of a program, each task in the process will have threads to specific execution.

thread
Single-threaded concurrent operations

Dry for two days, the worker is not A happy work too much to add the money, then pharaoh at one dry is slow, go outside and hired A B workers, the workers B also corresponds to A single thread, Lao wang let workers continue to move A brick, and workers B to carry wood, then two tasks can be carried out at the same time, this is multithreaded parallel operation. Moreover, worker A and worker B share the resources provided by Lao Wang at the same time.

3.3 Processes and Threads + single-core and multi-core

In fact, Lao Wang’s example already explains the operation of processes and threads, but it is not too cautious.

  • If we were using a single-core CPU, we would not be able to perform parallel operations, only concurrent operations, so that the FULL use of CPU resources.
  • If we use a multi-core CPU, we can truly parallelize operations.

Our operating system in the process or switching between threads is a resource consuming, because want to save the current state of the running context information, and process or thread switching is determined by the operating system, after arrived at run time, the operating system will hang the currently executing task, we can only wait for the next slice of time allocated to continue to perform a task. This leads to the concept of coroutines.

3.4 coroutines Coroutine

As we mentioned earlier, task switching is controlled by the operating system. Is there any way we can reduce this overhead? We can use coroutines, which we can think of as lightweight threads. Coroutine execution is not handled directly by the operating system, but is determined by the compiler. For example, if coroutine A says that my current task will be completed for A period of time, and I can give up the resources currently occupied, coroutine A will notify the scheduler, and the scheduler will assign the next coroutine to execute. See this link for more information on coroutines.

The above is some of my summary, if there is any improper place welcome to discuss together. Welcome to pay attention to our public number: knowledge precipitation tribe.