Asynchronous programming under Linux-thread overview and detailed usage (1) [recommended for newbies]

I. Introduction

For interactive client applications, users always hope that the program can respond to UI operations at all times; for high-performance server development, users always hope that the server can handle multiple requests at the same time... etc. At this time, we can use multi-threading technology to ensure UI threads can respond, increase server throughput, increase program processing speed, and set task priorities for scheduling.

Multithreading technology is just that multiple threads are executed in different time slices allocated by the operating system. It is not that the program opens 12 threads and 12 threads are executed at the same "point in time". How many threads can be executed at the same "point in time" is determined by the CPU , The connection of each execution thread is scheduled by the operating system. That is, in the case that the number of threads exceeds the number of processors used to process them, the operating system will periodically schedule a time slice for each thread to control the processor, thereby simulating simultaneous concurrency.

Before we understand threads, we need to understand the CPU and the process.

Two, multi-core CPU hyper-threaded CPU

1、多核心处理器(CPU)

Refers to a processor (CPU) containing multiple processing units, each processing unit is equivalent to a single-core processor (CPU). Therefore, the function of the multi-core processor is equivalent to the online combat of multiple single-core processor computers.

2、超线程处理器(CPU)

Refers to the virtual method of simulating a physical core into multiple cores in a CPU (usually a single physical core is simulated as two cores, also known as two threads. Only when the number of threads is more than the number of physical cores It can be called hyperthreading. For example, four cores and four threads are not hyperthreading, but four cores and eight threads can be called hyperthreading).

3、 优缺点:

  • 1) Multi-core is the real physical core. A multi-core processor (CPU) is equivalent to multiple single-core processors (CPU) cooperating with each other. Therefore, in theory, multi-core has higher computing power than hyper-threading. Although the computing speed of multi-core is much faster than hyper-threading, multi-core also has an obvious disadvantage, that is, the use efficiency of multi-core is lower than that of hyper-threading processor (CPU). This is because when multi-cores process data, they do not "cooperate" perfectly with each other. Often a core needs to wait for the calculation data of other cores, which delays time and is forced to slow down. In addition, since the current multi-core uses a shared cache, this slows down the multi-core CPU operation speed a lot (because: the CPU reads the Cache in a row unit, if the two hardware threads are different The memory is located in the same Cache row, so when two hardware threads are writing to their respective memory at the same time, it will cause the problem of two hardware threads writing the same Cache row, which will cause competition).
  • 2) Hyper-threading uses a virtual method to virtualize a physical core into multiple cores. It can maximize the use of existing core resources and has a high cost performance.

3. Operating system support for multi-core processors

Mainly reflected in scheduling and interruption:

  1. Optimize the assignment of tasks. Try to make the tasks of the same application execute on the same core.
  2. Optimize shared data for tasks. As the multi-core processor (ChipMulti-Processor, CMP) architecture shares the cache (currently), you can consider changing the data distribution of the task in the memory to maximize the hit rate of the cache when the task is executed.
  3. Load balancing optimization of tasks. When tasks are being scheduled, load imbalance occurs. Consider migrating the tasks that are least related to other tasks among the busy processors to minimize data conflicts.
  4. Operating systems that support preemptive multitasking can create the effect of simultaneous execution of multiple threads in multiple processes. It does this by dividing the available processor time among threads that require processor time, and assigning processor time slices to each thread in turn. The currently executing thread is suspended at the end of its time slice, while another thread continues to run. When the system switches from one thread to another, it will save the thread context of the preempted thread and reload the saved thread context of the next thread in the thread queue.

Fourth, the relationship between processes and threads

1. Process

A process is an execution instance of an application. Each process is composed of private virtual address space, code, data, and various other system resources. The resources created by the process during its operation are destroyed as the process terminates. System resources are released or closed when the process terminates.

2. Thread

A thread is an execution unit within a process. After the system creates a process, it actually starts the main execution thread that executes the process. When the main thread of execution terminates, the process terminates accordingly.

Each thread maintains an exception handler, scheduling priority, and thread context. (Thread context, the currently executing thread is suspended at the end of its time slice, while another thread continues to run. When the system switches from one thread to another, it will save the thread context of the preempted thread and restart Load the saved thread context of the next thread in the thread queue)

3. Relationship

The operating system uses processes to separate the different applications they are executing. The .NET Framework further subdivides the operating system processes into lightweight managed sub-processes of System.AppDomain (application domain).

线程是CPU的调度单元,是进程中的执行单位,一个进程中可以有多个线程同时执行代码

The editor recommends my own linuxC/C++ language technology exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them!

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/113122216