[C++] Qt-thread process

Article directory

process

In the Task Manager, you can see that under the Process Tab page, all processes are divided into three categories:

  1. Application: Open and running software.
  2. Background process: Hide in the background and run quietly.
  3. windows process: various services that the operating system relies on to start and run

Various software and processes depend on the operating system to run.

A program is an executable code stored in memory. When we double-click the shortcut, the operating system will take the code out of the memory and start executing it, and assign a PID to each process. Some applications on the Windows operating system can open multiple applications, such as folders, browsers, etc., while some applications only allow one to be opened, such as the computer version of WeChat. Generally, only one APP on the mobile phone system is allowed to be opened.

Process concept: It is a dynamic execution process of a program with certain independent functions on a data set. It is an independent basic unit for resource allocation and scheduling by the operating system, and is the carrier for application running.

Process is an abstract concept and there has never been a unified standard definition.

Process components: program, data collection, process control block.

Processes have 4 characteristics:

  1. Dynamic: A process is an execution process of a program. It is temporary, has a life cycle, is generated dynamically, and dies dynamically.
  2. Concurrency: Any process can execute concurrently with other processes.
  3. Independence: A process is an independent unit of the system for resource allocation and scheduling.
  4. Structural: The process consists of three parts: program, data and process control block.

The relationship between applications and processes: As you can see from the details page of the task manager, an application can start multiple processes. PID (part ID) is a unique identifier of the process, just like a person's ID number. Multiple processes under an application are in a tree structure, and the number with the smallest PID is the root node. There is an option to end the process tree by right-clicking on a process. If you choose to kill the process tree of the root node, the processes of all child nodes will be killed.

Thread

In early operating systems, there was no concept of threads. A process is the smallest unit that has resources and can run independently. It is also the smallest unit of program execution. Later, with the development of computers, the requirements for the CPU became higher and higher, and the cost of switching between processes was high, which could no longer meet the requirements of increasingly complex programs, so threads were invented.

Thread concept: The smallest basic unit that the CPU can perform scheduling, allocation, execution, and calculations. It is a single sequential control process in program execution. A process can have one or more threads, and each thread shares the memory space of the process.

  1. A process is similar to a factory and is the basic unit for the system to allocate resources. A thread is similar to a worker in a factory and is the basic unit for the CPU to schedule and execute work.
  2. A process consists of one or more threads.
  3. Processes are independent of each other, but the memory space of the program (including code segments, data sets, heaps, etc.) is shared between threads in the same process.
  4. Scheduling and switching: Thread context switching is much faster than process context switching.

In order to complete the task faster, or in some scenarios where multiple things need to be done at the same time, you need to use threads, because threads can perform tasks "simultaneously".

Here I hope to display the switching between processes first, and then display the threads.

- [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-3FbuQpR3-1688772078589) (C++.assets/image-20230704051159177.png)]

  • Serial: In order, after one execution, the next one will be executed.
  • Parallel: at the same time, refers to execution at the same time.
  • Concurrency: Occurs within the same time interval, refers to the same time interval, and is executed alternately.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-iZ1SL1ci-1688772078590) (C++.assets/image-20230704051509159.png)]

In a single thread, it is executed in a serial manner.

The task scheduling of most operating systems adopts a preemptive scheduling method of rotating time slices. One thread executes for a short period of time and then pauses to rest and waits to be awakened. The next thread is awakened and starts executing, and each thread executes alternately. A small period of time during which a thread executes is called a time slice.

Since the execution speed of the CPU is very fast and the time slice is very short, the rapid switching between threads gives the impression that multiple threads are "running at the same time", which is often referred to as concurrency.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-P2WAEpdO-1688772078590) (C++.assets/image-20230704051843300.png)]

Thread status:

  • New ecology: Create new thread objects.
  • Ready state: After creating a thread and entering the ready state, the thread will be added to the ready queue and wait for the CPU time slice to be allocated before entering the running state.
  • Running state: If the time slice of a thread in the running state is used up, it will enter the ready state again. Generally speaking, the ready state and the running state do not require human participation and are scheduled by the operating system. If sleep, wait, suspend, IO are encountered When requested, it will enter the blocking state.
  • Blocked state (suspended state): A running thread will give up the right to use the CPU and temporarily suspend its execution under certain special circumstances, such as being artificially suspended or performing time-consuming I/O operations. Entering the blocking state, a thread in the blocking state cannot enter the queuing queue. Only when the cause of the blocking is eliminated can the thread be transferred to the ready state. When the thread is restored, the IO operation is completed, and the resource is waited for, it will enter the ready state.
  • Death state: The thread ends normally, exits due to exception, is forcibly terminated, and the process ends its life cycle.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-6zCGAJ7g-1688772078591) (C++.assets/image-20230704052825968.png)]

Notice:

  1. A thread must be assigned a time slice through the ready state before it can enter the running state, but cannot directly enter the running state.
  2. The ready state cannot enter the blocking state.
  3. Threads in other states can directly enter the death state.

Create thread

Create a Qt console project, use the API function CreateThread under window, Header: #include<windows.h>

::CreateThread(nullptr/*默认的安全属性*/,
                   0/*windows 默认1M*/,
                   &ThreadProc/*线程函数的地址*/,
                   &n/*线程函数的参数*/,
                   0/*0:立即运行 ,SUS_PEND 挂起*/,
                   nullptr/*线程ID*/);

The thread function can be found by looking for the third parameter

DWORD WINAPI ThreadProc (LPVOID lpThreadParameter){
    
    
    int nn = *(int*)lpThreadParameter;
    for(int i=0;i<nn;i++){
    
    
        qDebug()<<"挣钱----"<<i;
        Sleep(1000);
    }

    return 0;
}

Write a similar loop in the main function

    for(int i=0;i<20;i++){
    
    
        qDebug()<<"-------------------睡觉:"<<i;
        Sleep(1000);
    }

Through testing, we can find that we create a new thread instead of simply calling the thread function

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-T4w89Mno-1688772078591) (C++.assets/image-20230704055213979.png)]

Guess you like

Origin blog.csdn.net/jia_03/article/details/131607459