Thread processing method and device, equipment, storage medium and product

By delaying the thread of the background application until the terminal processor load decreases and gradually executing the thread exit process, the problem of excessive processor load caused by excessive system resources by background applications is solved, and the effect of reducing processor load and scheduling overhead is achieved.

CN119862001APending Publication Date: 2025-04-22GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311373472.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-20
Publication Date
2025-04-22

AI Technical Summary

Technical Problem

In the terminal, applications running in the background accumulate a large amount of system resources, resulting in excessive processor load, which in turn causes problems such as increasing CPU instantaneous load, increasing scheduling overhead and locking.

Method used

Provides a thread processing method, by setting an exit flag for multiple threads when receiving an exit signal from a background application, and when detecting that the processor load is higher than a preset value, it delays the thread's wake-up until the load decreases, and then gradually wakes up and executes the thread's exit process.

Benefits of technology

It reduces the instantaneous load of the terminal processor, reduces the scheduling overhead and lock competition when multiple threads execute the exit process, alleviates the locking problem, and improves the stability and efficiency of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119862001A_ABST
    Figure CN119862001A_ABST
Patent Text Reader

Abstract

The invention provides a thread processing method and device, equipment, a storage medium and a product, and belongs to the field of thread processing. The method comprises the steps that when an exit signal for a first application program running in the background is received, exit marks are set for a plurality of first threads corresponding to the first application program respectively, and the exit marks of the first threads are used for representing that the first threads need to be exited; detecting a load of a processor of the terminal; when it is detected that the load of the processor is higher than the preset load, periodically detecting the load of the processor; when it is detected that the load of the processor is not higher than the preset load, multiple first threads corresponding to the first application program are awakened, and based on the exit marks of the multiple first threads, an exit process is executed on the multiple first threads. According to the method, the instantaneous load of the processor of the terminal can be reduced, so that the scheduling overhead for executing the exit process of the plurality of first threads is reduced, and the locking problem is relieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of thread processing, and in particular to a thread processing method, device, equipment, storage medium and product. Background Art

[0002] In order to facilitate the user to use the terminal application next time, when the user switches the first application to the second application, the first application will not be closed, but will be switched to the background to run; however, after the terminal has been used for a period of time, a large number of applications will accumulate in the background of the terminal, which will inevitably occupy a large amount of system resources of the terminal. When the system resources of the terminal are insufficient, it is necessary to kill the applications running in the background to release system resources. When killing the applications running in the background, it is necessary to wake up the thread corresponding to the application, and then execute the exit process of the thread corresponding to the application.

[0003] However, one application may include hundreds of threads. When a large number of applications running in the background are checked and killed, the number of threads will be very large. Waking up a large number of threads will cause a very high instantaneous CPU load on the terminal, which will in turn increase the scheduling overhead of the exit process of the threads corresponding to the executing application and cause problems such as locking. Summary of the invention

[0004] The embodiments of the present application provide a thread processing method, apparatus, device, storage medium and product, which can reduce the instantaneous load of the processor of the terminal, thereby reducing the scheduling overhead of executing the exit process of multiple first threads and alleviating the locking problem. The technical solution is as follows:

[0005] In one aspect, a thread processing method is provided, the method comprising:

[0006] When an exit signal for a first application program running in the background is received, an exit mark is set for each of the plurality of first threads corresponding to the first application program, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited;

[0007] Detecting the load of the processor of the terminal;

[0008] When it is detected that the load of the processor is higher than a preset load, periodically detecting the load of the processor;

[0009] When it is detected that the load of the processor is not higher than the preset load, the multiple first threads corresponding to the first application are awakened, and based on the exit marks of the multiple first threads, an exit process is executed for the multiple first threads.

[0010] On the other hand, a thread processing method device is provided, the device comprising:

[0011] A setting module, configured to, when receiving an exit signal for a first application program running in the background, respectively set an exit mark for a plurality of first threads corresponding to the first application program, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited;

[0012] A detection module, used to detect the load of the processor of the terminal;

[0013] A delay module, configured to periodically detect the load of the processor when it is detected that the load of the processor is higher than a preset load;

[0014] A processing module is used to wake up multiple first threads corresponding to the first application when it is detected that the load of the processor is not higher than the preset load, and execute an exit process for the multiple first threads based on the exit marks of the multiple first threads.

[0015] On the other hand, a terminal is provided, which includes one or more processors and one or more memories, wherein at least one program code is stored in the one or more memories, and the at least one program code is loaded and executed by the one or more processors to implement the thread processing method as described above.

[0016] On the other hand, a computer-readable storage medium is provided, wherein at least one program code is stored in the storage medium, and the at least one program code is loaded and executed by a processor to implement the thread processing method described above.

[0017] On the other hand, a computer program product is provided. The computer program product stores at least one program code. The at least one program code is used to be executed by a processor to implement the thread processing method described above.

[0018] In an embodiment of the present application, when the load of the processor of the terminal is large, the awakening of multiple first threads corresponding to the application running in the background is temporarily suspended; when the load of the processor is small, the multiple first threads are awakened, and then the exit process is executed for the multiple first threads, that is, the present application can realize delayed awakening, which can avoid the situation where the processor load is too large when exiting the application running in the background, and reduce the instantaneous load of the processor. Since the instantaneous load of the processor is reduced, the scheduling overhead when the competing processors are scheduled when the exit process is executed for multiple first threads is reduced, and the occurrence of lock competition for the resources required for the competition when the exit process is executed for multiple first threads can also be reduced. It can be seen from this that the present application can reduce the scheduling overhead of executing the exit process for multiple first threads and alleviate the problem of stuck locks. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1A flowchart of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0020] Figure 2 A flowchart of a thread processing method in a related art shown in an exemplary embodiment of the present application is shown;

[0021] Figure 3 A schematic diagram of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0022] Figure 4 A schematic diagram of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0023] Figure 5 A flowchart of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0024] Figure 6 A flowchart of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0025] Figure 7 A flowchart of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0026] Figure 8 A schematic diagram of a thread processing method shown in an exemplary embodiment of the present application is shown;

[0027] Fig. 9 A schematic diagram showing a processor load condition of a thread processing method in a related art shown in an exemplary embodiment of the present application;

[0028] Fig.10 A schematic diagram showing a processor load condition of a thread processing method of the present application scheme shown in an exemplary embodiment of the present application;

[0029] Fig.11 A block diagram of a thread processing device shown in an exemplary embodiment of the present application is shown;

[0030] Fig.12 A block diagram of a terminal according to an exemplary embodiment of the present application is shown. DETAILED DESCRIPTION

[0031] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.

[0032] The term "multiple" as used herein refers to two or more than two. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the related objects are in an "or" relationship.

[0033] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, stored data, displayed data, etc.) and signals involved in this application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions. For example, the processor load, first thread, second thread and third thread involved in this application are all obtained when fully authorized.

[0034] Please refer to Figure 1 , which shows a flowchart of a thread processing method shown in an exemplary embodiment of the present application. The execution subject of the method is a terminal, and the method includes:

[0035] Step 101: When an exit signal for a first application running in the background is received, an exit mark is set for each of a plurality of first threads corresponding to the first application, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited.

[0036] The exit signal is used to instruct the first application to exit the background operation. The terminal includes an upper-level killing management process (killer), which periodically detects the remaining system resources of the terminal; when the remaining system resources of the terminal are less than the preset resources, the killer sends an exit signal for the first application running in the background to the terminal. The first application can be all applications running in the background of the terminal, or it can be part of the applications running in the background of the terminal; when the first application is part of the applications running in the background, the killer determines the first application whose foreground running time is farthest from the current time from the applications running in the background.

[0037] After the terminal sets the exit mark for the multiple first threads, it does not immediately wake up the multiple first threads, but first executes step 102 to detect the load of the processor of the terminal, and determines whether to delay waking up the multiple first threads based on the load of the processor; the exit mark can be the TIF_SIGPENDING mark. In addition, the present application delays waking up the multiple first threads corresponding to the first application running in the background, but does not delay waking up the threads of other emergency scenarios, thereby avoiding the situation where the application freezes due to delayed waking up of the threads in other emergency scenarios.

[0038] For example, see Figure 2 In the related art, when the terminal receives the exit signal sent by the killer, the terminal sets exit marks for multiple first threads, then wakes up the multiple first threads, and executes the exit process for the multiple first threads based on the exit marks of the multiple first threads. Since in the related art, when the terminal receives the exit signal, the multiple first threads are immediately woken up, while in the present application, when the terminal receives the exit signal but the load of the processor of the terminal is large, the multiple first threads are not woken up immediately, but are woken up with a delay; therefore, in order to facilitate the comparison of the differences between the wake-up process of the related art and the present application, Figure 2 It only describes the wake-up process, but does not describe the exit process after wake-up.

[0039] The process of executing the exit process for the multiple first threads based on the exit marks of the multiple first threads by the terminal may be: when any of the multiple first threads obtains processor resources and is scheduled by the processor, the first thread is returned from the kernel space to the user space, and in the process of returning the first thread from the kernel space to the user space, it is detected whether the exit mark is set for the first thread; when it is detected that the exit mark is set for the first thread, the exit process is executed for the first thread. When it is detected that the exit mark is not set for the first thread, the exit process is not executed for the first thread.

[0040] When the number of first threads is large, waking up multiple first threads at the same time will cause the processor to be overloaded. When the processor is overloaded, multiple first threads will compete for processor resources when waiting to be scheduled by the processor, resulting in more preemptive scheduling, that is, the problem of high scheduling cost. For example, when a small number of first threads need to be exited in the terminal, these first threads can all obtain processor resources, and then the terminal can execute the exit process for these first threads based on the processor resources. When a large number of first threads need to be exited in the terminal, a large number of first threads need to be in a ready (runable) state, that is, a state of waiting to be exited. When these first threads compete for processor resources, they will exit the state of waiting to be exited, but when these first threads do not compete for processor resources, these first threads will enter the state of waiting to be exited again, so these first threads may have to go through repeated several times of entering and exiting the state of waiting to be exited, that is, these first threads may have to go through repeated several times of switching in and out, and each time the process of switching in and out corresponds to two scheduling behaviors, so the problem of high scheduling cost will appear in the related technology.

[0041] In addition, when the terminal executes the exit process for multiple first threads, it needs to execute the same code process (code process corresponding to the exit process) for multiple first threads. When executing the same code process, it is necessary to execute the lock process to lock the resources, and then execute the same code process based on the locked resources, that is, multiple first threads will have lock competition. On the one hand, once multiple first threads have lock competition, the lock processes of multiple first threads need to be executed based on the lock slow path. The lock slow path means that multiple first threads need to sleep first and wait to be awakened again before they can execute the lock process. Each first thread executes the lock process based on the lock slow path, which adds two schedulings. The intense lock competition caused by the high load of the processor almost causes each first thread to be exited to have lock competition, thereby bringing additional scheduling overhead. On the other hand, a large number of first threads are blocked in the lock competition. At this time, if the second thread corresponding to the application running in the foreground also tries to lock, it will have to wait for a large number of first threads in lock competition to exit before locking the resources based on the lock process. If the second thread corresponding to the application running in the foreground is a high-priority thread related to screen drawing, the subsequent rendering process cannot be performed due to the inability to lock resources, which will cause the application running in the foreground to freeze. A typical scenario is the competition for the control group (cgroup) lock, that is, the second thread competes for the cgroup lock.

[0042] For example, see Figure 3 At time T0, the killer sends an exit signal to the terminal, which is used to wake up N first threads. After a period of time, most of the N first threads are waiting for the cgroup lock due to fierce competition for the cgroup lock. At time T1, the second thread corresponding to the application running in the foreground requests the cgroup lock. Then the second thread corresponding to the application running in the foreground has to wait for these N first threads to complete the exit process before it can get the cgroup lock and continue to execute the subsequent process. In the scenario where a large number of applications exit, the value of N may be 5000. Setting it to 10000 will obviously cause the application running in the foreground to freeze.

[0043] Step 102: Detect the load of the processor of the terminal.

[0044] In some embodiments, a first application corresponds to a process, and a process includes multiple first threads, that is, a first application corresponds to multiple first threads; when the first application exits, the terminal needs to wake up the multiple first threads corresponding to the first application and execute the exit process for the multiple first threads. When executing the exit process for multiple first threads, the terminal adds the multiple first threads to the scheduling queue, and the processor of the terminal sequentially schedules the multiple first threads in the scheduling queue based on the storage order of the multiple first threads stored in the scheduling queue. The scheduling of the multiple first threads in the scheduling queue by the processor of the terminal will increase the load of the processor, that is, the more the number of first threads to be scheduled in the scheduling queue, the greater the load of the processor; accordingly, the step of detecting the load of the processor of the terminal can be: determining the number of first threads stored in the scheduling queue of the terminal, the scheduling queue is used to store the first threads to be executed in the exit process; based on the number of first threads stored in the scheduling queue, determining the load of the processor of the terminal. In some embodiments, the number of first threads stored in the scheduling queue is used as the load of the processor of the terminal. Correspondingly, when the number of first threads stored in the scheduling queue is greater than the preset number, the terminal determines that the load of the processor is higher than the preset load, and executes step 103, that is, delaying the awakening of multiple first threads; when the number of first threads stored in the scheduling queue is not greater than the preset number, the terminal determines that the load of the processor is not higher than the preset load, wakes up the multiple first threads corresponding to the first application, and executes the exit process for the multiple first threads, that is, there is no need to delay the awakening of multiple first threads.

[0045] In some embodiments, during the process of the terminal exiting the first application, when receiving an exit signal for the second application running in the background, the terminal will also wake up multiple third threads corresponding to the second application (the wake-up here may also be a delayed wake-up), and execute the exit process for the multiple third threads; and when executing the exit process for the multiple third threads, the multiple third threads are added to the scheduling queue, and the scheduling queue is not only used to store the first thread of the exit process to be executed, but also used to store the third thread of the exit process to be executed. In this case, the step of detecting the load of the processor of the terminal can be: determining the number of the first thread and the third thread stored in the scheduling queue of the terminal, and determining the load of the processor of the terminal based on the number of the first thread and the third thread stored in the scheduling queue.

[0046] For example, see Figure 4, the killer sends an exit signal for the first application to the terminal. Based on the exit signal of the first application, the terminal determines that it is necessary to wake up multiple first threads (Childt1, Child t2...Childtn) included in the process corresponding to the first application. After waking up the multiple first threads, the terminal stores the multiple first threads in a scheduling queue (runqueue, rq). At this time, rq includes Runable t1, Runable t2...Runable tn waiting to be scheduled, that is, the number of first threads to be executed in the exit process stored in rq is the load of the processor.

[0047] Step 103: When it is detected that the load of the processor is higher than a preset load, periodically detect the load of the processor.

[0048] When it is detected that the processor load is higher than the preset load, it means that the instantaneous load of the processor is already very large, and it is determined that the delayed wake-up process needs to be started. At this time, the multiple first threads corresponding to the first application are temporarily awakened, that is, multiple first threads are not awakened at this time, but a timer is created and the timing cycle of the timer is set; when the timer reaches the timing cycle, the processor load is detected again. When the processor load detected again is not higher than the preset load, step 104 is executed, and the next timing cycle of the timer is waited; when the processor load detected again is higher than the preset load, the next timing cycle of the timer is waited.

[0049] In some embodiments, the timing period of the timer can be set and changed as needed; for example, the timing period of the timer can be 3 milliseconds or 4 milliseconds. In some embodiments, when the refresh frequency of the terminal is 250 Hz, the terminal may not create a timer separately, but reuse the time base (tick) interrupt of the terminal system, that is, whenever the tick triggers an interrupt, the terminal detects the load of the processor. Since the terminal reuses the tick interrupt, there is no need to create a timer separately, which reduces the thread processing overhead.

[0050] Step 104: When it is detected that the load of the processor is not higher than the preset load, the multiple first threads corresponding to the first application are awakened, and based on the exit marks of the multiple first threads, the exit process is executed for the multiple first threads.

[0051] In some embodiments, when it is detected that the load of the processor is not higher than the preset load, the terminal can wake up multiple first threads corresponding to the first application at one time, and based on the exit marks of the multiple first threads, execute the exit process for the multiple first threads at one time, thereby improving the efficiency of exiting the first application. In some embodiments, the terminal can also wake up the multiple first threads corresponding to the first application in batches, and execute the exit process for the multiple first threads in batches; accordingly, step 104 can be: when it is detected that the load of the processor is not higher than the preset load, determine the second number of first threads from the multiple first threads, wake up the second number of second threads, and execute the exit process for the second number of second threads based on the exit marks of the second number of second threads. Then continue to detect the load of the processor of the terminal, and when it is detected that the load of the processor is not higher than the preset load next time, determine the second number of first threads again from the remaining first threads to be awakened, wake up the second number of second threads determined again, and execute the exit process for the second number of second threads determined again based on the exit marks of the second number of second threads determined again, until all first threads are awakened, and stop detecting the load of the processor.

[0052] One point that needs to be explained is that when the number of remaining first threads to be awakened is less than the second number, the terminal can awaken the remaining first threads to be awakened, and based on the exit mark of the remaining first threads to be awakened, execute the exit process for the remaining first threads to be awakened. At this time, the terminal determines that all first threads are awakened and stops detecting the processor load.

[0053] For example, please refer to Figure 4 When the terminal executes an exit process for any first thread, it needs to execute the exit code corresponding to the exit process, and the core function of the exit code may be do_exit; and the exit code is used to release and exit the relevant resources of the first thread; for example, the terminal releases and exits the statistical resources, signal resources, memory resources and other related resources of the first thread based on the Id lock competition (profile_task_exit), control group lock competition (exit_signals) and memory lock competition (exit_mm) in do_exit.

[0054] In an embodiment of the present application, when the load of the processor of the terminal is large, the awakening of multiple first threads corresponding to the application running in the background is temporarily suspended; when the load of the processor is small, the multiple first threads are awakened, and then the exit process is executed for the multiple first threads, that is, the present application can realize delayed awakening, which can avoid the situation where the processor load is too large when exiting the application running in the background, and reduce the instantaneous load of the processor. Since the instantaneous load of the processor is reduced, the scheduling overhead when the competing processors are scheduled when the exit process is executed for multiple first threads is reduced, and the occurrence of lock competition for the resources required for the competition when the exit process is executed for multiple first threads can also be reduced. It can be seen from this that the present application can reduce the scheduling overhead of executing the exit process for multiple first threads and alleviate the problem of stuck locks.

[0055] Please refer to Figure 5 , which shows a flowchart of a thread processing method shown in an exemplary embodiment of the present application. The method includes:

[0056] Step 501: When receiving an exit signal for a first application running in the background, the terminal sets an exit mark for a plurality of first threads corresponding to the first application, where the exit mark of the first thread is used to indicate that the first thread needs to be exited.

[0057] In some embodiments, this step is the same as step 101 and will not be repeated here.

[0058] Step 502: The terminal detects the load of the processor of the terminal.

[0059] In some embodiments, this step is the same as step 102 and will not be repeated here.

[0060] When it is detected that the load of the processor is higher than the preset load, step 503 is executed; when it is detected that the load of the processor is not higher than the preset load, step 513 is executed.

[0061] Step 503: When it is detected that the load of the processor is higher than the preset load, the terminal caches the multiple first threads for which the exit mark has been set in the to-be-awakened queue, and the to-be-awakened queue is used to cache the first threads to be awakened.

[0062] During the process of the terminal exiting the first application, when an exit signal for the second application running in the background is received, an exit mark is also set for the multiple third threads corresponding to the second application, and then the multiple third threads are cached in the queue to be awakened. At this time, the queue to be awakened is not only used to cache the first thread to be awakened, but also used to cache the third thread to be awakened. It should be noted that during the process of the terminal exiting the first application, multiple threads corresponding to new applications to be exited (for example, multiple third threads corresponding to the second application) may be continuously added to the queue to be awakened.

[0063] Step 504: The terminal creates a timer and sets a timing period of the timer.

[0064] Step 505: When the timing period of the timer is reached, the terminal detects the load of the processor again.

[0065] After the terminal caches multiple first threads with exit marks set to the wake-up queue, it creates a timer and sets the timing period of the timer. When the timing period of the timer is reached, step 505 is executed to detect the processor load again, and then through steps 506-511, the multiple first threads in the wake-up queue are awakened in batches, and based on the exit marks of the multiple first threads, the exit process is executed for the multiple first threads in batches.

[0066] In some embodiments, before exiting the first application, the terminal has delayed waking up multiple fourth threads corresponding to the third application. At this time, a timer has been created. There is no need to create a timer again in this step. The timing can be performed based on the created timer.

[0067] When it is detected that the load of the processor is not higher than the preset load, step 506 is executed; when it is detected that the load of the processor is higher than the preset load, step 514 is executed.

[0068] Step 506: When it is detected that the load of the processor is not higher than the preset load, the terminal wakes up a first number of first threads in the queue to be awakened based on the arrangement order of the first threads in the queue to be awakened, and deletes the awakened first threads from the queue to be awakened.

[0069] Based on the arrangement order of the first threads in the queue to be awakened, the terminal determines the first number of first threads that are first cached in the queue to be awakened, and then wakes up the first number of first threads. In some embodiments, the first number may be a pre-set number; for example, the first number may be 60, that is, each time it is detected that the load of the terminal is not higher than the preset load, 60 first threads are awakened. Since the first number is set in advance, the terminal does not need to determine the first number on the spot through processor resources, which saves processor resources and reduces the load of the processor.

[0070] In some other embodiments, the first number can be calculated by the terminal according to the current load of the processor; accordingly, the calculation process of the first number includes: the terminal determines the first number based on the current load and the preset load of the processor, and the determined first number needs to satisfy: the load of the processor when waking up the first number of threads is not higher than the first preset load. In the embodiment of the present application, the terminal can also calculate the number of first threads that can be awakened this time according to the real-time load of the processor, and can wake up as many first threads as possible, thereby improving the wake-up efficiency of the first thread.

[0071] Step 507: the terminal stores the first number of first threads in a scheduling queue of the terminal, where the scheduling queue is used to store the first threads of the to-be-executed exit process.

[0072] When the first number of first threads are awakened, it is necessary to execute an exit process for the first number of first threads. At this time, the terminal stores the first number of first threads in the scheduling queue of the terminal, and the processor of the terminal schedules the first threads in sequence based on the arrangement order of the first threads in the scheduling queue, and then executes an exit process for the scheduled first threads.

[0073] Step 508: When any first thread in the scheduling queue is scheduled by the processor, the terminal detects the exit mark of the first thread during the process of returning the first thread from the kernel space to the user space.

[0074] For any first thread in the scheduling queue, when the terminal executes the exit process for the first thread, the first thread is returned from the kernel space to the user space, that is, the first thread is released; at this time, the terminal will also detect the exit mark of the first thread to determine whether the exit mark is set for the first thread; when the exit mark of the first thread is detected, step 509 is executed; when the exit mark of the first thread is not detected, the exit process for the first thread is stopped.

[0075] Step 509: When the exit mark of the first thread is detected, the terminal executes the exit process of the first thread.

[0076] The terminal executes an exit code corresponding to the exit process, and the core function of the exit code may be do_exit; and the exit code is used to release and exit the relevant resources of the first thread; for example, release and exit the statistical resources, signal resources, memory resources and other relevant resources of the first thread.

[0077] Step 510: The terminal determines whether all first threads are awakened.

[0078] Step 511 : when there is still a first thread that has not been awakened, the terminal waits for the next timing cycle of the timer, and then executes step 505 .

[0079] Step 512: When all first threads are awakened, the terminal deletes the timer and stops detecting the load of the processing.

[0080] Step 513: When it is detected that the load of the processor is not higher than the preset load, the terminal wakes up the multiple first threads corresponding to the first application, and executes an exit process for the multiple first threads based on the exit marks of the multiple first threads.

[0081] Step 514: When it is detected that the processor load is higher than the preset load, wait for the next timing cycle of the timer, and then execute step 505.

[0082] For example, see Figure 6 When the terminal receives the exit signal sent by the killer for the first application running in the background, the terminal sets an exit mark for the multiple first threads corresponding to the first application, and then detects the load of the processor of the terminal (rq load), and determines whether the load of the processor is higher than the preset load; when the load of the processor is not higher than the preset load, wake up a part of the first threads, and based on the exit marks of the awakened part of the first threads, execute the exit process for the part of the first threads. When the load of the processor is higher than the preset load, create a timer and set the timing period of the timer. When the timing period of the timer is reached, the terminal will detect the load of the processor again; when it is detected again that the load of the processor is not higher than the preset load, wake up a part of the first threads again, and based on the exit marks of the part of the first threads awakened again, execute the exit process for the part of the first threads awakened again, and then wait for the next timing period of the timer. When the load of the processor detected again is higher than the preset load, the terminal waits for the next timing period of the timer again.

[0083] In some embodiments, each time the terminal performs a wake-up operation, it determines whether all the first threads are awakened; when all the first threads are awakened, the timer is deleted and the detection of the processing load is stopped; when there are still first threads that have not been awakened, the terminal waits for the next timing cycle of the timer, and so on, until all the first threads are awakened, the timer is deleted, and the detection of the processor load is stopped.

[0084] In some embodiments, during the process of the terminal exiting the first application, when an exit signal for the second application running in the background is received, an exit mark will be set for multiple third threads corresponding to the second application, and then the multiple third threads will be cached in the wake-up queue. That is, during the process of the first application being exited, multiple threads corresponding to new applications to be exited (for example, the third thread corresponding to the second application) will be continuously added to the wake-up queue. At the same time, the terminal will continuously wake up the first thread and / or the third thread in the wake-up queue according to the processor load until the first thread and the third thread in the wake-up queue are both awakened. At this time, the timer will be deleted and the processor load detection will stop.

[0085] In an embodiment of the present application, whenever it is detected that the load of the processor is not higher than the preset load, a portion of the first thread is awakened, so that the peak load of the processor can be smoothed and the load of the processor can be averaged, so that the processor can maintain a reasonable workload.

[0086] The following introduces the theoretical basis for the delayed awakening of multiple first threads in the embodiment of the present application from the two points of the processor load and whether it will affect the exit process of the first thread:

[0087] First, from the perspective of the processor, the processing power of the processor is constant; for example, the number of first threads corresponding to the first application is 6000, and the processor can execute the exit process of 1500 first threads within 1 second; then according to the scheme in the related art, waking up 6000 first threads at the same time, at least 4 seconds are required to complete the exit process of 6000 first threads. However, if 6000 first threads are woken up at the same time, and 6000 first threads are stored in the scheduling queue to be queued for scheduling by the processor, this is entirely possible to cause higher scheduling overhead, which may bring longer processing time. According to the delayed wake-up scheme in the embodiment of the present application, if only 1500 first threads are woken up per second, 6000 first threads also need 4 seconds to process, thereby ensuring that the time required for the exit of the first application is the same, but the embodiment of the present application reduces the scheduling overhead of executing the exit process for 6000 first threads because the load of the processor is averaged. Moreover, the timing period of the timer in the embodiment of the present application is 3 milliseconds or 4 milliseconds, that is, the delayed wake-up of the present application is in milliseconds instead of seconds. Therefore, the granularity of the delayed wake-up of the present application is smaller, which can not only reduce the scheduling overhead of the exit process of the 6000 first threads, but also achieve more precise control.

[0088] Second, the present application can achieve the control of the peak load of the processor, and the present application only delays the wake-up time of multiple first threads to be exited. When the terminal receives the exit signal sent by the killer, the terminal will still set the exit mark for multiple first threads, so that the fifth thread that is executing the exit process or the sixth thread that is already in the scheduling queue waiting to execute the exit process can be executed normally, and will not cause the fifth thread and / or sixth thread that should have exited to occupy the processor's running time. In addition, in the scenario where the first application is exited, the multiple first threads corresponding to the first application are rarely in the process of executing the exit process or are already in the scheduling queue waiting to execute the exit process, which will not cause the first thread to be delayed in exiting.

[0089] Please refer to Figure 7 , which shows a flowchart of a thread processing method shown in an exemplary embodiment of the present application. The method includes:

[0090] Step 701: When receiving an exit signal for a first application running in the background, the terminal sets an exit mark for a plurality of first threads corresponding to the first application, where the exit mark of the first thread is used to indicate that the first thread needs to be exited.

[0091] In some embodiments, this step is the same as step 101 and will not be repeated here.

[0092] Step 702: The terminal detects the load of the processor of the terminal.

[0093] In some embodiments, this step is the same as step 102 and will not be repeated here.

[0094] Step 703: When it is detected that the load of the processor is higher than the preset load, the terminal caches the multiple first threads for which the exit mark has been set in the to-be-awakened queue, and the to-be-awakened queue is used to cache the first threads to be awakened.

[0095] In some embodiments, this step is the same as step 503 and will not be repeated here.

[0096] Step 704: The terminal wakes up the multiple first threads in the queue to be awakened in batches.

[0097] The terminal creates a timer and sets a timing period of the timer; when the timing period of the timer is reached, the terminal detects the load of the processor again. When it is detected again that the load of the processor is not higher than the preset load, the terminal wakes up the first number of first threads in the queue to be awakened based on the arrangement order of the first threads in the queue to be awakened, and deletes the awakened first threads from the queue to be awakened. The terminal waits for the next timing period of the timer, and when the next timing period of the timer is reached, the terminal detects the load of the processor again. When it is detected again that the load of the processor is not higher than the preset load, the terminal wakes up the first number of first threads in the queue to be awakened based on the arrangement order of the first threads in the queue to be awakened, until all the first threads in the queue to be awakened have been awakened, deletes the timer, and stops detecting the load of the processor.

[0098] Step 705: For any first thread in the first batch currently executing the exit process, the terminal locks the target resource and executes the exit process for the first thread based on the target resource.

[0099] Based on the lock request of the first thread, the terminal locks the requested target resource, and based on the target resource, executes the exit code corresponding to the exit process. The core function of the exit code may be do_exit; and the exit code is used to release and exit the relevant resources of the first thread; for example, release and exit the statistical resources, signal resources, memory resources and other relevant resources of the first thread.

[0100] Step 706: When receiving a lock request for the target resource from the second thread, after executing the exit processes of multiple first threads in the first batch, the terminal responds to the lock request, and the priority level of the second thread is higher than the priority level of the first thread to execute the exit process.

[0101] The second thread may be a thread corresponding to the application running in the foreground; for example, the second thread may be a thread for executing screen drawing. In the embodiment of the present application, after executing the exit process of multiple first threads in the first batch, the lock request of the second thread can be responded to without waiting for all first threads to complete the exit process, thereby improving the response to the lock request of the second thread and avoiding the occurrence of freezing of the application running in the foreground.

[0102] The following introduces the theoretical basis for the delayed awakening of the first thread in the embodiment of the present application from the point of lock contention:

[0103] Since the present application can reduce the load of the processor, the reduced load of the processor can reduce the occurrence of a large number of first threads competing for resources, that is, the delayed wake-up of the present application can reduce lock contention; since the lock contention is reduced, the lock holding process of more first threads does not need to follow the slow lock holding path, that is, these first threads do not need to sleep first and then wait to be awakened, thereby saving the time for locking resources based on the lock holding request and the scheduling overhead of the processor scheduling the first thread.

[0104] In addition, after reducing the processor load, this application can also avoid the occurrence of freezing of applications running in the foreground. For example, please refer to Figure 8 The terminal adopts a batch delayed wake-up scheme for multiple first threads. In the early stage of executing the exit process for multiple first threads, due to the low load of the processor, the first threads included in the first batch (batch1) are awakened, and the exit process is executed for the first threads included in batch1. However, due to the high load of the processor, the terminal delays the awakening of the first threads included in batch2-batchN in the subsequent batch2-batchN. If a second thread (the thread corresponding to the application running in the foreground) comes in during the T0 stage, then in the worst case, it only needs to wait for the terminal to complete the exit process of the first thread included in batch1. The same situation can be extended to the subsequent T1 stage and TN stage. In the related art, in the worst case, the terminal needs to wait until all the first threads have completed the exit process before responding to the lock request of the second thread.

[0105] The second thread may be a thread corresponding to the application running in the foreground; for example, the second thread may be a thread for executing screen drawing. In the embodiment of the present application, after executing the exit process of multiple first threads in the first batch, the lock request of the second thread can be responded to without waiting for all first threads to complete the exit process, thereby improving the response to the lock request of the second thread and avoiding the occurrence of freezing of the application running in the foreground.

[0106] The following diagram vividly shows the load of the terminal processor when multiple first threads are awakened by two methods; please refer to Fig. 9 and Fig.10 , Fig. 9 is the load condition of the processor when waking up multiple first threads through the solution in the related art, Fig.10 This is the load condition of the processor when multiple first threads are awakened by the solution of the present application. Fig. 9 and Fig.10 The horizontal axis is time, and the vertical axis is the number of first threads in the processor's scheduling queue. The terminal collects a point every 5 milliseconds. Fig. 9 and Fig.10By comparing the above, it can be clearly seen that when multiple first threads are awakened by the solution of the embodiment of the present application, the peak load of the processor is flattened. At the same time, from the synchronously collected processor scheduling data and the second thread jamming data, it can be seen that the embodiment of the present application can reduce the number of scheduling times during the exit of the first application by 10%, and reduce the lock holding delay of the high-priority second thread by 90%, thereby reducing the occurrence of jamming of the foreground application corresponding to the second thread, that is, reducing the frame loss rate of the foreground application corresponding to the second thread, thereby improving the user experience.

[0107] Please refer to Fig.11 , which shows a block diagram of a thread processing device shown in an exemplary embodiment of the present application, the device includes:

[0108] A setting module 1101 is used for setting exit marks for a plurality of first threads corresponding to the first application respectively when an exit signal for the first application running in the background is received, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited;

[0109] A detection module 1102, configured to detect a load of a processor of a terminal;

[0110] The delay module 1103 is used to periodically detect the load of the processor when it is detected that the load of the processor is higher than a preset load;

[0111] The processing module 1104 is used to wake up the multiple first threads corresponding to the first application when it is detected that the load of the processor is not higher than the preset load, and execute the exit process for the multiple first threads based on the exit marks of the multiple first threads.

[0112] In some embodiments, the delay module 1103 is further used to cache multiple first threads that have set exit marks into a queue to be awakened, and the queue to be awakened is used to cache the first threads to be awakened;

[0113] The processing module 1104 is used to wake up the multiple first threads in the queue to be awakened in batches, and execute exit procedures for the multiple first threads in batches based on the exit marks of the multiple first threads.

[0114] In some embodiments, the processing module 1104 is used to wake up a first number of first threads in the queue to be awakened based on the arrangement order of the first threads in the queue to be awakened, and delete the awakened first threads from the queue to be awakened when it is detected that the load of the processor is not higher than the preset load this time; when it is detected that the load of the processor is not higher than the preset load next time, wake up the first number of first threads in the queue to be awakened again based on the arrangement order of the first threads in the queue to be awakened, until all the first threads in the queue to be awakened have been awakened, and stop detecting the load of the processor.

[0115] In some embodiments, the processing module 1104 is used to lock the target resource for any first thread in the first batch currently executing the exit process, and execute the exit process of the first thread based on the target resource.

[0116] In some embodiments, the apparatus further comprises:

[0117] The lock processing module 1104 is used to respond to the lock request when receiving a lock request for the target resource from the second thread, after executing the exit processes of multiple first threads in the first batch, and the priority level of the second thread is higher than the priority level of the first thread to execute the exit process.

[0118] In some embodiments, the processing module 1104 is used to store multiple first threads in a scheduling queue of the terminal, and the scheduling queue is used to store the first thread of the exit process to be executed; when any first thread in the scheduling queue is scheduled by the processor, the exit mark of the first thread is detected in the process of returning the first thread from the kernel space to the user space; when the exit mark of the first thread is detected, the exit process is executed for the first thread.

[0119] In some embodiments, the detection module 1102 is used to determine the number of first threads stored in the scheduling queue of the terminal, and the scheduling queue is used to store the first thread of the exit process to be executed; based on the number of first threads stored in the scheduling queue, determine the load of the processor of the terminal.

[0120] In an embodiment of the present application, when the load of the processor of the terminal is large, the awakening of multiple first threads corresponding to the application running in the background is temporarily suspended; when the load of the processor is small, the multiple first threads are awakened, and then the exit process is executed for the multiple first threads, that is, the present application can realize delayed awakening, which can avoid the situation where the processor load is too large when exiting the application running in the background, and reduce the instantaneous load of the processor. Since the instantaneous load of the processor is reduced, the scheduling overhead when the competing processors are scheduled when the exit process is executed for multiple first threads is reduced, and the occurrence of lock competition for the resources required for the competition when the exit process is executed for multiple first threads can also be reduced. It can be seen from this that the present application can reduce the scheduling overhead of executing the exit process for multiple first threads and alleviate the problem of stuck locks.

[0121] It should be noted that the thread processing device provided in the above embodiment only uses the division of the above functional modules as an example when performing thread processing. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the terminal is divided into different functional modules to complete all or part of the functions described above. In addition, the thread processing device provided in the above embodiment and the thread processing method embodiment belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.

[0122] Please refer to Fig.12 , which shows a block diagram of a terminal 1200 shown in an exemplary embodiment of the present application. The terminal 1200 in the present application includes one or more of the following components: a processor 1210 , a memory 1220 , and a display screen 1230 .

[0123] The processor 1210 may include one or more processing cores. The processor 1210 uses various interfaces and lines to connect the various parts of the entire terminal 1200, and executes various functions and processes data of the terminal 1200 by running or executing instructions, programs, code sets or instruction sets stored in the memory 1220, and calling data stored in the memory 1220. Optionally, the processor 1210 can be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 1210 can integrate one or more combinations of a central processing unit (CPU), a graphics processing unit (GPU), a neural network processor (NPU), and a modem. Among them, the CPU mainly processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing the content that needs to be displayed on the display screen 1230; the NPU is used to implement artificial intelligence (AI) functions; and the modem is used to process wireless communications. It is understandable that the above-mentioned modem may not be integrated into the processor 1210, but may be implemented by a separate chip.

[0124] The memory 1220 may include a random access memory (RAM) or a read-only memory (ROM). Optionally, the memory 1220 includes a non-transitory computer-readable storage medium. The memory 1220 may be used to store instructions, programs, codes, code sets, or instruction sets. The memory 1220 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc.; the data storage area may store data (such as audio data, a phone book), etc., created according to the use of the terminal 1200.

[0125] The display screen 1230 is a display component for displaying a user interface. Optionally, the display screen 1230 is a display screen with a touch function, through which the user can use any suitable object such as a finger, a touch pen, etc. to perform touch operations on the display screen 1230.

[0126] The display screen 1230 is usually arranged on the front panel of the terminal 1200. The display screen 1230 can be designed as a full screen, a curved screen, a special-shaped screen, a double-sided screen or a folding screen. The display screen 1230 can also be designed as a combination of a full screen and a curved screen, a combination of a special-shaped screen and a curved screen, etc., which is not limited in this embodiment.

[0127] In addition, those skilled in the art can understand that the structure of the terminal 1200 shown in the above drawings does not constitute a limitation on the terminal 1200, and the terminal 1200 may include more or fewer components than shown, or combine certain components, or arrange the components differently. For example, the terminal 1200 also includes an audio acquisition device, a speaker, a radio frequency circuit, an input unit, a sensor, an audio circuit, a wireless fidelity (Wireless Fidelity, Wi-Fi) module, a power supply, a Bluetooth module and other components, which will not be repeated here.

[0128] An embodiment of the present application further provides a computer-readable medium, which stores at least one program code, and the at least one program code is loaded and executed by the processor to implement the thread processing method shown in the above embodiments.

[0129] An embodiment of the present application further provides a computer program product, which stores at least one program code, and the at least one program code is loaded and executed by the processor to implement the thread processing method shown in the above embodiments.

[0130] In some embodiments, the computer program product involved in the embodiments of the present application may be deployed and executed on one terminal, or on multiple terminals located at one location, or on multiple terminals distributed at multiple locations and interconnected by a communication network. Multiple terminals distributed at multiple locations and interconnected by a communication network may constitute a blockchain system.

[0131] Those skilled in the art should be aware that in one or more of the above examples, the functions described in the embodiments of the present application can be implemented with hardware, software, firmware, or any combination thereof. When implemented using software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium. Computer-readable media include computer storage media and communication media, wherein the communication media include any media that facilitates the transmission of a computer program from one place to another. The storage medium can be any available medium that a general or special-purpose computer can access.

[0132] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.

Claims

1. A thread processing method, characterized in that: The method comprises: When an exit signal for a first application program running in the background is received, an exit mark is set for each of the plurality of first threads corresponding to the first application program, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited; Detecting the load of the processor of the terminal; When it is detected that the load of the processor is higher than a preset load, periodically detecting the load of the processor; When it is detected that the load of the processor is not higher than the preset load, the multiple first threads corresponding to the first application are awakened, and based on the exit marks of the multiple first threads, an exit process is executed for the multiple first threads.

2. The method according to claim 1, characterized in that The method further comprises: Cache the plurality of first threads for which exit marks have been set in a queue to be awakened, wherein the queue to be awakened is used to cache the first threads to be awakened; The waking up the multiple first threads corresponding to the first application and executing an exit process for the multiple first threads based on the exit marks of the multiple first threads includes: The multiple first threads in the queue to be awakened are awakened in batches, and based on the exit marks of the multiple first threads, the exit process is executed in batches for the multiple first threads.

3. The method according to claim 2, characterized in that The step of waking up the plurality of first threads in the queue to be awakened in batches comprises: When it is detected that the load of the processor is not higher than the preset load, based on the arrangement order of the first threads in the queue to be awakened, awakening a first number of first threads in the queue to be awakened, and deleting the awakened first threads from the queue to be awakened; When it is detected next time that the load of the processor is not higher than the preset load, based on the arrangement order of the first threads in the queue to be awakened, the first number of first threads in the queue to be awakened are awakened again until all the first threads in the queue to be awakened have been awakened, and the load detection of the processor is stopped.

4. The method according to claim 2, characterized in that: The step of executing the exit process on the multiple first threads in batches based on the exit marks of the multiple first threads includes: For any first thread in the first batch currently executing the exit process, a target resource is locked, and based on the exit tags of the multiple first threads, the exit process is executed on the first thread through the target resource.

5. The method according to claim 4, characterized in that The method further comprises: When receiving a lock request for the target resource from a second thread, after executing the exit procedures of multiple first threads in the first batch, the second thread responds to the lock request, and the priority level of the second thread is higher than the priority level of the first thread to execute the exit procedure.

6. The method according to claim 1, characterized in that The step of executing an exit process for the plurality of first threads based on the exit marks of the plurality of first threads includes: Storing the multiple first threads in a scheduling queue of the terminal, where the scheduling queue is used to store the first thread to be executed for the exit process; When any first thread in the scheduling queue is scheduled by the processor, in the process of returning the first thread from the kernel space to the user space, detecting an exit mark of the first thread; When the exit mark of the first thread is detected, the exit process is executed for the first thread.

7. The method according to claim 1 or 6, characterized in that: The detecting terminal processor load includes: Determine the number of first threads stored in a scheduling queue of the terminal, wherein the scheduling queue is used to store first threads to execute the exit process; The load of the processor of the terminal is determined based on the number of first threads stored in the scheduling queue.

8. A thread processing device, characterized in that: The device comprises: A setting module, configured to, when receiving an exit signal for a first application program running in the background, respectively set an exit mark for a plurality of first threads corresponding to the first application program, wherein the exit mark of the first thread is used to indicate that the first thread needs to be exited; A detection module, used to detect the load of the processor of the terminal; A delay module, configured to periodically detect the load of the processor when it is detected that the load of the processor is higher than a preset load; A processing module is used to wake up multiple first threads corresponding to the first application when it is detected that the load of the processor is not higher than the preset load, and execute an exit process for the multiple first threads based on the exit marks of the multiple first threads.

9. A terminal, characterized in that: The terminal includes one or more processors and one or more memories, wherein at least one program code is stored in the one or more memories, and the at least one program code is loaded and executed by the one or more processors to implement the thread processing method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: At least one program code is stored in the storage medium, and the at least one program code is loaded and executed by the processor to implement the thread processing method according to any one of claims 1 to 7.

11. A computer program product, characterized in that The computer program product stores at least one program code, and the at least one program code is used to be executed by a processor to implement the thread processing method according to any one of claims 1 to 7.