Thread control method, device, electronic device and storage medium
By increasing the priority of low-priority threads, the critical thread blocking problem caused by multi-thread access to the same memory is solved, and the higher smooth operation of electronic devices is achieved.
Patent Information
- Application Number
- CN202211160275.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-22
- Publication Date
- 2025-09-02
- Estimated Expiration
- 2042-09-22
AI Technical Summary
When multiple threads access the same piece of memory at the same time, the critical thread blocking time is too long, affecting the smoothness of the electronic device.
By increasing the priority of the currently held low-priority threads in the high-priority threads waiting for the target lock, the operation of the low-priority threads is accelerated, so that the high-priority threads hold the lock faster and improve the smoothness of the operation.
It effectively reduces the waiting time of critical threads, improves the smooth operation of electronic devices, and avoids the impact on resource allocation of other threads.
Smart Images

Figure CN115543612B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of electronic devices, and more specifically, to a thread control method, device, electronic device, and storage medium. Background Art
[0002] With the rapid advancement of technology and living standards, electronic devices (such as smartphones and tablets) have become commonplace in people's lives, and people's expectations for the smooth operation of these devices are increasing. In related technologies, electronic devices are running an increasing number of threads, and memory resources are becoming increasingly scarce. If multiple threads access the same memory block simultaneously, critical threads may be blocked for extended periods, affecting the smooth operation of the electronic device. Summary of the Invention
[0003] This application proposes a thread control method, device, electronic device and storage medium, which can improve the running smoothness of key threads.
[0004] In a first aspect, an embodiment of the present application provides a thread control method, which is applied to an electronic device, and the method includes: when a first thread is waiting to hold a first target lock, determining a second thread that currently holds the first target lock, wherein the thread priority of the first thread is higher than the first priority; if the thread priority of the second thread is lower than the second priority, then increasing the thread priority of the second thread, and the second priority is lower than the first priority.
[0005] In a second aspect, an embodiment of the present application provides a thread control device, comprising: a thread determination module and a priority adjustment module, wherein the thread determination module is used to determine the second thread currently holding the first target lock when the first thread is waiting to hold the first target lock, wherein the thread priority of the first thread is higher than the first priority; the priority adjustment module is used to increase the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, and the second priority is lower than the first priority.
[0006] In a third aspect, an embodiment of the present application provides an electronic device comprising: one or more processors; a memory; and one or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, and the one or more applications are configured to execute the thread control method provided in the first aspect above.
[0007] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which program code is stored. The program code can be called by a processor to execute the thread control method provided in the first aspect above.
[0008] The solution provided by this application determines the second thread currently holding the first target lock when the first thread is waiting to hold the first target lock. The thread priority of the first thread is higher than the first priority. If the thread priority of the second thread is lower than the second priority, the thread priority of the second thread is increased. The second priority is lower than the first priority. In this way, when the first thread with a thread priority higher than the first priority is waiting to hold the lock, the thread priority of the second thread currently holding the lock, whose thread priority is lower than the second priority, is increased, thereby speeding up the operation of the second thread, allowing the first thread to hold the lock more quickly, and thus improving the running smoothness of the first thread. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0010] Figure 1 A flow chart of a thread control method according to an embodiment of the present application is shown.
[0011] Figure 2 A flow chart of a thread control method according to another embodiment of the present application is shown.
[0012] Figure 3 A flow chart of a thread control method according to another embodiment of the present application is shown.
[0013] Figure 4 A flow chart of a thread control method according to another embodiment of the present application is shown.
[0014] Figure 5 A block diagram of a thread control device according to an embodiment of the present application is shown.
[0015] Figure 6 It is a block diagram of an electronic device for executing the thread control method according to an embodiment of the present application.
[0016] Figure 7 It is a storage unit for storing or carrying program codes for implementing the thread control method according to an embodiment of the present application. DETAILED DESCRIPTION
[0017] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0018] With the rapid development of electronic devices, consumers are pursuing gorgeous mobile phone appearance and rich operating experience, while also increasing their demand for smooth operation of electronic devices. How to regulate system resources related to the smooth operation of electronic devices is also becoming increasingly important. Among them, as the user's usage time increases, the system background increases, and memory resources gradually become scarce, if multiple threads access the same memory at the same time, the key thread will be blocked for a long time, thereby affecting the smooth operation of the electronic device. For example, in the related art, when a thread acquires a mutex, if the lock is held by another task, the thread that wants to get the lock will be placed in a waiting queue. After the thread that gets the lock releases the lock, the task in the waiting queue will be awakened and executed. Therefore, when the user is operating the interface, the thread loading content in the foreground is the key thread (with a higher thread priority). The thread loading content in the foreground and multiple threads in the background access the same memory at the same time, which will cause the foreground thread to be blocked for a long time, thereby causing lag.
[0019] To address the above issues, the inventors have proposed the thread control method, apparatus, electronic device, and storage medium provided in the embodiments of this application. By increasing the thread priority of a second thread, currently holding the lock, whose thread priority is lower than the second priority, while a first thread, whose thread priority is higher than a first priority, is waiting to acquire a lock, the second thread's execution is accelerated, allowing the first thread to acquire the lock more quickly, thereby improving the first thread's operational smoothness. The specific thread control method is described in detail in the subsequent embodiments.
[0020] The thread control method provided in the embodiment of the present application is described in detail below with reference to the accompanying drawings.
[0021] See also Figure 1 , Figure 1 FIG. 1 is a flow chart showing a thread control method provided by an embodiment of the present application. In a specific embodiment, the thread control method is applied to Figure 5 The thread control device 400 and the electronic device 100 ( Figure 6 ). The following will take electronic devices as an example to illustrate the specific process of this embodiment. Of course, it is understandable that the electronic devices used in this embodiment can be smart phones, tablet computers, smart watches, e-books, laptop computers, etc., and are not limited here. Figure 1 The process shown is described in detail, and the thread control method may specifically include the following steps:
[0022] Step S110: When a first thread waits to hold a first target lock, a second thread currently holding the first target lock is determined, wherein the thread priority of the first thread is higher than the first priority.
[0023] Among them, the first thread can be any thread created in the electronic device with a thread priority higher than the first priority. The first priority can be a pre-set thread priority. When the thread priority of a process is higher than the first priority, it can be considered that the thread priority of the process is higher and it can be regarded as a critical thread. For example, the thread priority is divided into 1 to 10. When the thread priority is 1, the thread priority is the lowest, and when the thread priority is 10, the thread priority is the highest. In this case, the first priority can be 6, 7, 8, etc. Of course, the specific value of the first priority can be not limited.
[0024] The first target lock can be a read-write lock, a mutex lock, a conditional lock, or a spin lock, and its type is not limited. In the operating system of an electronic device, multiple threads are typically running in parallel at the same time, and multiple threads may access the same resource simultaneously. Therefore, multiple processes in the kernel also require some synchronization mechanism to synchronize access to shared data by each execution unit. This leads to the lock mechanism in the Linux system. A lock is a permission, and a thread can only access the resource corresponding to the lock after obtaining the lock.
[0025] In an embodiment of the present application, when the first thread is waiting to hold the first target lock, the first thread is in a blocked state and needs to wait for the first target lock to be held before it can run. In this case, the thread currently holding the first target lock can be determined (as the second thread) so as to control the second thread so that the first thread can hold the first target lock faster, thereby ensuring the process execution of the first thread with high thread priority.
[0026] In some implementations, for some types of locks, the lock holder may not be recorded in the Linux system. Therefore, the electronic device records the thread currently holding the lock so that the thread currently holding the lock can be determined based on the recorded information.
[0027] Step S120: If the thread priority of the second thread is lower than a second priority, increase the thread priority of the second thread, and the second priority is lower than the first priority.
[0028] In an embodiment of the present application, after the electronic device determines the second thread currently holding the first target lock, it can determine whether the thread priority of the second thread is lower than the second priority. If the thread priority of the second thread is lower than the second priority, it means that the thread priority of the second thread is relatively low, and its criticality in the threads created by the electronic device is relatively low. Among them, the second priority can be a pre-set thread priority lower than the first priority. For example, the thread priority is divided into 1 to 10. When the thread priority is 1, the thread priority is the lowest, and when the thread priority is 10, the thread priority is the highest. In this case, the second priority can be 2, 3, 4, etc. Of course, the specific value of the second priority can be not limited.
[0029] It is understandable that the thread priority of a thread in the operating system of an electronic device determines the operating resources allocated to the thread. The higher the thread priority, the more operating resources the operating system allocates to the thread. Conversely, the lower the thread priority, the lower the operating resources the operating system allocates to the thread. The operating resources may be processor resources, such as central processing unit (CPU) resources, graphics processing unit (GPU) resources, etc. Therefore, when the thread priority of the second thread currently holding the first target lock is lower than the second priority, the operating resources allocated to the second thread are less due to the lower thread priority. Therefore, the thread priority of the second thread can be increased to increase the operating resources allocated to the second thread to avoid interruption of the operation, thereby speeding up the operation of the second thread, and then enabling the first thread to hold the first target lock faster, ensuring that the first thread with a high thread priority can hold the first target lock faster.
[0030] In some implementations, if the thread priority of the second thread is higher than or equal to the second priority, it means that the thread priority of the second thread is not low, and the running process of the second thread is not easily interrupted, so it may not be processed.
[0031] For example, in some scenarios, the interface displayed by the electronic device is a sliding list, and the first thread is the thread that loads the list content in the foreground. At this time, the thread priority of the first thread is higher than the first priority, that is, the system hopes that the first thread will run quickly to ensure the smooth operation of the electronic device; at this time, the first thread and other threads may need to access the same memory at the same time, and the second thread is holding the lock to access the memory. Based on the above implementation method, the first thread can hold the lock to access the memory faster, thereby ensuring the smoothness of content loading when the user slides the list.
[0032] The thread control method provided in the embodiment of the present application determines the second thread currently holding the first target lock when the first thread is waiting to hold the first target lock. The thread priority of the first thread is higher than the first priority. If the thread priority of the second thread is lower than the second priority, the thread priority of the second thread is increased. The second priority is lower than the first priority. In this way, when the first thread with a thread priority higher than the first priority is waiting to hold the lock, the thread priority of the second thread currently holding the lock, which has a thread priority lower than the second priority, is increased, thereby speeding up the operation of the second thread, allowing the first thread to hold the lock more quickly, and thus improving the running smoothness of the first thread.
[0033] See also Figure 2 , Figure 2 A flow chart of a thread control method provided by another embodiment of the present application is shown. The thread control method is applied to the above electronic device. Figure 2 The process shown is described in detail, and the thread control method may specifically include the following steps:
[0034] Step S210: When a first thread waits to hold a first target lock, a second thread currently holding the first target lock is determined, wherein the thread priority of the first thread is higher than the first priority.
[0035] In the embodiment of the present application, step S210 can refer to the contents of other embodiments and will not be repeated here.
[0036] Step S220: If the thread priority of the second thread is lower than the second priority, determine the remaining resources of the current electronic device, and the second priority is lower than the first priority.
[0037] In an embodiment of the present application, when it is determined that the thread priority of the second thread currently holding the first target lock is lower than the second priority, the remaining resources of the electronic device can be determined to improve the thread priority of the second thread based on the remaining resources of the electronic device. The remaining resources of the electronic device can be the remaining operating resources of the electronic device. It can be understood that the amount of the remaining resources of the electronic device can determine the operating resources that can be increased and allocated to the second thread. Therefore, the remaining resources of the electronic device can be obtained to specifically improve the thread priority of the second thread.
[0038] Step S230: If the remaining resources are greater than the first preset resources, the thread priority of the second thread is increased to the thread priority of the first thread.
[0039] After determining the remaining resources of the electronic device, the remaining resources can be compared with the first preset resources. If the remaining resources of the electronic device are more than the first preset resources, it means that the remaining resources of the electronic device can meet the increased operating resources required to increase the thread priority of the second thread to the thread priority of the first thread; conversely, if the remaining resources of the electronic device are less than or equal to the first preset resources, it means that the remaining resources of the electronic device cannot meet the increased operating resources required to increase the thread priority of the second thread to the thread priority of the first thread. Among them, the first preset resources are pre-set, and their specific values may not be limited. For example, if the remaining resources are the remaining usage rate of the CPU, the first preset resources can be 20%, 40%, 60%, etc.
[0040] In an embodiment of the present application, if it is determined that the remaining resources of the electronic device are more than the first preset resources, the thread priority of the second thread can be increased to the thread priority of the first thread, so that the thread priority of the second thread is the same as the thread priority of the first thread, that is, the thread priority of the first thread is transferred to the second thread, so that the importance of the second thread currently holding the first target lock is the same as the importance of the first thread waiting to hold the first target lock, so that the electronic device can run the second thread at the same running speed, and thus the first thread can hold the first target lock faster.
[0041] In some embodiments, the thread priority of the first thread may be lower than the highest thread priority. In this case, if the above remaining resources are more than the first preset resources, and the remaining resources are less than the third preset resources, the thread priority of the second thread is increased to the thread priority of the first thread, and the third preset resources are more than the first preset resources. In addition, if the thread priority of the second thread is lower than the second priority, the thread priority of the second thread is increased, and further includes: if the remaining resources are more than the first preset resources, and the remaining resources are equal to or less than the third preset resources, the thread priority of the second thread is increased to the highest thread priority.
[0042] In this embodiment, if the remaining resources are not less than the third preset resources, it means that the remaining resources of the electronic device can meet the increased operating resources required to increase the thread priority of the second thread to the highest thread priority; conversely, if the remaining resources are less than the third preset resources, it means that the remaining resources of the electronic device cannot meet the increased operating resources required to increase the thread priority of the second thread to the highest thread priority. The third preset resources can be more than the first preset resources. For example, if the first preset resources are 40%, the third preset resources can be 50%, 60%, etc. Therefore, when the remaining resources are more than the first preset resources and the remaining resources are less than the third preset resources, the thread priority of the second thread can be increased to the thread priority of the first thread; and when the remaining resources are not less than the third preset resources, the thread priority of the second thread can be increased to the highest thread priority, so that the second thread can run faster, thereby further reducing the time the first thread waits for the lock.
[0043] Step S240: If the remaining resources are less than or equal to the first preset resources, and the remaining resources are more than the second preset resources, the thread priority of the second thread is increased to a third priority, the second preset resources are less than the first preset resources, the third priority is lower than the thread priority of the first thread, and the third priority is higher than the thread priority of the second thread before adjustment.
[0044] In an embodiment of the present application, if the remaining resources of the electronic device are less than or equal to the first preset resources, and the remaining resources are more than the second preset resources, it means that the remaining resources of the electronic device cannot meet the increased operating resources required to increase the thread priority of the second thread to the thread priority of the first thread, but the thread priority of the second thread can be increased relatively slightly (relative to the thread priority of the first thread) to avoid affecting the operation of other threads. Therefore, in this case, the thread priority of the second thread can be increased to the third priority, and the third priority is lower than the thread priority of the first thread, and the third priority is higher than the thread priority of the second thread before adjustment (that is, the original thread priority of the second thread). For example, the thread priority of the first thread is 8 and the thread priority of the second thread is 3. When the remaining resources of the electronic device are less than or equal to the first preset resources, and the remaining resources are more than the second preset resources, the thread priority of the second thread can be increased to 7, 6, 5, etc.
[0045] The thread control method provided in an embodiment of the present application determines the remaining resources of the electronic device when a first thread is waiting to hold a first target lock and, if the thread priority of a second thread currently holding the first target lock is lower than the second priority, then specifically increases the thread priority of the second thread based on the remaining resources. This not only speeds up the operation of the second thread, allowing the first thread to hold the lock more quickly, thereby improving the running smoothness of the first thread, but also ensures the smooth operation of the electronic device without affecting the operation of other threads.
[0046] See also Figure 3 , Figure 3 A flowchart of a thread control method provided by another embodiment of the present application is shown. The thread control method is applied to the above electronic device. In the embodiment of the present application, the first target lock can be a read-write lock. Figure 3 The process shown is described in detail, and the thread control method may specifically include the following steps:
[0047] Step S310: Record all second threads currently holding the first target lock in a preset queue.
[0048] In an embodiment of the present application, the lock that the first thread is waiting to hold may be a read-write lock (e.g., an rwsem lock). Since the relevant code of the read-write lock in the related art does not record which threads hold the read-write lock, it is impossible to adjust the priority of the thread currently holding the lock. Therefore, a preset queue can be maintained in the electronic device, and the preset queue records all threads currently holding the read-write lock. Therefore, the electronic device can record all second threads currently holding the first target lock in the preset queue, so that when a thread with a high thread priority is waiting to hold the first target lock, the thread currently holding the first target lock can be determined based on the preset queue.
[0049] Step S320: When the first thread waits to hold the first target lock, determine a second thread currently holding the first target lock based on the preset queue, wherein the thread priority of the first thread is higher than the first priority.
[0050] In an embodiment of the present application, when a thread with a high thread priority (thread priority higher than the first priority) is waiting to hold the first target lock, since the electronic device maintains a preset queue that records threads holding read-write locks, the second thread currently holding the first target lock can be determined based on the preset queue.
[0051] In some embodiments, when the first target lock is a read-write lock, when the first thread enters the waiting queue waiting to hold the first target lock, since the thread priority of the first thread is higher than the first priority, the electronic device can also insert the first thread into the head of the waiting queue so that after the second thread releases the critical section, the first thread can hold the first target lock faster.
[0052] In some embodiments, when the first target lock is a read-write lock, when the first thread enters the waiting queue to hold the first target lock, the threads in the waiting queue may be sorted in descending order of thread priority, and the first thread may be inserted into the corresponding position in the waiting queue. For example, the waiting queue includes thread 1, thread 2, and thread 3, and the thread priorities of thread 1, thread 2, and thread 3 decrease in order. Then, thread 1, thread 2, and thread 3 are arranged in order from the head of the waiting queue. When the first thread enters the waiting queue, if the thread priority of the first thread is higher than the thread priority of thread 2 and lower than the thread priority of thread 1, the first thread is inserted between thread 1 and thread 2.
[0053] Step S330: If the thread priority of the second thread is lower than a second priority, increase the thread priority of the second thread, and the second priority is lower than the first priority.
[0054] In some embodiments, when the read-write lock is occupied by the process reading data, that is, when the read lock occupies the critical section, since the read lock is shared, there may be multiple second threads holding the first target lock and occupying the critical section at the same time. Therefore, if the thread priorities of multiple second threads are increased at the same time, it may result in too many running resources being allocated to the second threads, resulting in too few running resources being available to other processes, thereby causing them to starve to death. Therefore, when the electronic device determines that the thread priority of the second thread is lower than the second priority, it can determine the number of second threads in the preset queue, that is, the number of threads currently holding the first target lock; after determining the number of second threads, it can compare the number of second threads with the preset number. If the number of second threads is less than or equal to the preset number, the thread priority of all second threads can be increased; if the number of second threads is greater than the preset number, the thread priority of the target thread (part of the second threads) in the preset queue is increased to avoid too many threads having their thread priorities increased, resulting in too few resources being obtained by other threads. Among them, when increasing the thread priority of the second thread, the method of adjusting the thread priority of the second thread in the aforementioned embodiment can be referred to, and will not be repeated here.
[0055] In one possible implementation, if the thread priority of the second thread is lower than the second priority, and it is determined that the number of the second threads in the preset queue is greater than a preset number, the preset number of second threads can be obtained from the preset queue as the target threads based on the thread priority of each second thread, wherein the thread priority of the target thread is higher than the thread priority of other threads in the preset queue except the target thread; and the thread priority of the target thread in the preset queue is increased.
[0056] In this embodiment, when the number of second threads exceeds a preset number, when the thread priority of some second threads in the preset queue is increased, the second threads in the preset queue may be sorted in descending order of thread priority, and the first N threads in the sorted result are obtained as target threads, where N is equal to the preset number. Thus, by increasing the thread priority of target threads with higher thread priorities relative to other threads, the target threads with higher thread priorities relative to other threads can be executed faster than the other threads, allowing the critical section to be released more quickly, thereby reducing the time the first thread waits to hold the first target lock.
[0057] In one possible implementation, if the thread priority of the second thread is lower than the second priority, and it is determined that the number of the second threads in the preset queue is greater than a preset number, the preset number of second threads can be obtained from the preset queue as the target threads based on the thread priority of each second thread, wherein the thread priority of the target thread is lower than the thread priority of other threads in the preset queue except the target thread; and the thread priority of the target thread in the preset queue is increased.
[0058] In this embodiment, when the number of second threads exceeds a preset number, when raising the thread priority of some of the second threads in the preset queue, the second threads in the preset queue may be sorted in ascending order of thread priority, and the first N threads in the sorted result are obtained as target threads, where N is equal to the preset number. Thus, raising the thread priority of target threads with lower thread priorities relative to other threads can cause target threads with lower thread priorities to execute faster than other threads, allowing critical sections to be released more quickly, thereby reducing the time the first thread waits to hold the first target lock.
[0059] Step S340: If the second thread releases the critical section corresponding to the first target lock, the thread priority of the second thread is restored to the thread priority before adjustment.
[0060] In an embodiment of the present application, after the second thread holding the first target lock recorded in the preset queue releases the critical section corresponding to the first target lock, the first thread can hold the first target lock. At this time, the thread priority of the second thread can be restored to the thread priority before adjustment, so that the running resources it can obtain match its original thread priority, thereby ensuring the normal operation of other threads.
[0061] The following describes an example of the thread control method provided in the embodiment of the present application when applied to a read-write lock mechanism.
[0062] In related art, at time T1, a writer thread is waiting to acquire a write lock, and a first thread is waiting to acquire a read lock. At this point, multiple reader threads hold the read lock and occupy the critical section. After the multiple reader threads release the critical section, the waiting writer thread acquires the write lock and occupies the critical section. After the writer thread releases the critical section, the first thread acquires the read lock and occupies the critical section again. In this situation, if the thread priorities of these multiple reader and writer threads are lower than the second priority, the execution of these threads is likely to be interrupted, causing the first thread to take a longer time to acquire the first target lock.
[0063] Based on the thread control method provided in the embodiments of the present application, by recording the thread currently holding the first target lock in a preset queue, in the above example, when multiple reader threads hold the read lock and occupy the critical section, the multiple reader threads are treated as the above second threads, and the thread priority of the second threads is increased, thereby accelerating the release of the critical section by the multiple reader threads. Then, the writer thread holds the lock again, and the thread priority of the writer thread is also increased, thereby accelerating the release of the critical section by the writer thread. The first thread then holds the lock and reads the resources in the critical section. It can be seen that while the first thread is waiting for the lock, the waiting time of the first thread can be reduced.
[0064] The thread control method provided in an embodiment of the present application maintains a preset queue for recording threads holding a read-write lock. When a first thread is waiting to hold a first target lock, and the first target lock is a read-write lock, if the thread priority of a second thread currently holding the first target lock is lower than the second priority, the thread priority of the second thread is increased; and after the second thread releases the critical section corresponding to the first target lock, its thread priority is restored. This not only speeds up the execution of the second thread, allowing the first thread to hold the lock more quickly, thereby improving the running smoothness of the first thread, but also ensures the normal operation of other threads.
[0065] See also Figure 4 , Figure 4A flowchart of a thread control method provided by another embodiment of the present application is shown. The thread control method is applied to the above electronic device. In the embodiment of the present application, the first target lock can be a mutual exclusion lock. Figure 4 The process shown is described in detail, and the thread control method may specifically include the following steps:
[0066] Step S410: When a first thread waits to hold a first target lock, a second thread currently holding the first target lock is determined, wherein the thread priority of the first thread is higher than the first priority.
[0067] Step S420: If the thread priority of the second thread is lower than a second priority, increase the thread priority of the second thread, and the second priority is lower than the first priority.
[0068] Step S430: If the second thread is currently waiting to hold the second target lock, determine the third thread currently holding the second target lock, and the second target lock is a mutex lock.
[0069] In an embodiment of the present application, the lock that the first thread is waiting to hold may be a mutual exclusion lock (e.g., a mutex lock). A mutual exclusion lock is a simple locking method for controlling access to a shared resource. A mutual exclusion lock has only two states: locked and unlocked. If the mutex is already locked, the calling thread will block until the mutex is unlocked. After completing access to the shared resource, the mutex must be unlocked. In the mutual exclusion lock mechanism, a thread may attempt to acquire a mutual exclusion lock, while the thread currently holding the mutual exclusion lock may also be waiting to hold another mutual exclusion lock. In this case, the thread may also be in a blocked state. For example, when thread 1 with a high thread priority is waiting to acquire mutual exclusion lock a, mutual exclusion lock a is held by thread 2. At this time, thread 2 is also waiting to hold mutual exclusion lock b, so thread 2 is also in a blocked state. Only after thread 3 currently holding mutual exclusion lock b releases mutual exclusion lock b can thread 2 hold mutual exclusion lock b and be in a running state, and then thread 2 can release mutual exclusion lock a. Moreover, if the thread priority of thread 3 currently holding mutex b is low, thread 3 is easily interrupted during execution, causing thread 2 to wait for mutex b and thread 1 to wait for mutex a for a long time.
[0070] Therefore, when the thread priority of the second thread currently holding the first target lock is lower than the second priority, after the thread priority of the second thread is increased, it is also possible to determine whether the second thread is waiting to hold another mutex lock (the second target lock). If the second thread is waiting to hold the second target lock, it is also possible to determine the third thread currently holding the second target lock, so that by adjusting the thread priority of the third thread, the second thread can hold the second target lock faster, thereby releasing the first target lock faster. Among them, when increasing the thread priority of the second thread, the method of adjusting the thread priority of the second thread in the aforementioned embodiment can be referred to; when increasing the thread priority of the third thread, the method of adjusting the thread priority of the third thread in the aforementioned embodiment can be referred to, and no further details will be given here.
[0071] Step S440: If the thread priority of the third thread is lower than the second priority, increase the thread priority of the third thread.
[0072] In an embodiment of the present application, after determining the third thread that currently holds the second target lock, it can be determined whether the thread priority of the third thread is lower than the second priority; if the thread priority of the third thread is lower than the second priority, it means that the thread priority of the third thread is relatively low, and its criticality in the threads created by the electronic device is relatively low. Therefore, the thread priority of the third thread can be increased, so that the running resources that can be allocated to the third thread are increased, thereby increasing the running speed of the third thread, so that the second thread can hold the second target lock and complete the execution faster, and then the first thread can hold the first target lock released by the second thread after completing the operation more quickly.
[0073] In some embodiments, after the second thread releases the first target lock, the thread priority of the second thread is restored to the thread priority before adjustment (i.e., the original thread priority of the second thread). Similarly, after the third thread releases the second target lock, the thread priority of the third thread can also be restored to the thread priority before adjustment (i.e., the original thread priority of the third thread).
[0074] In some embodiments, if the third thread is also waiting to hold another mutex (the third target lock), that is, the third thread is also in a blocked state, and the reason for its being in a blocked state is that it is waiting to hold the other mutex, in this case, the fourth thread currently holding the third target lock can also be determined, and if the thread priority of the fourth thread is lower than the second priority, the thread priority of the fourth thread can be increased. That is to say, if the first thread is in a blocked state because it is waiting to hold a mutex, the thread holding the mutex can be searched for and the priority of the thread holding the mutex can be increased; and if the thread holding the mutex is also in a blocked state because it is waiting to hold another mutex, the thread holding the other mutex can continue to be searched for. Repeating this process can speed up the running speed of all related threads in the process of releasing the first target lock, thereby speeding up the release process of the first target lock and reducing the waiting time of the first thread waiting to hold the first target lock.
[0075] In one possible implementation, to prevent excessive thread priority increases from impacting the normal operation of other threads, the search depth may not exceed M levels, for example, 3 levels. After the fourth thread's priority level is increased, even if the fourth thread is blocked waiting for another mutex, the search for the thread currently holding the fourth target lock (the mutex the fourth thread is waiting to hold) will not be continued, i.e., no processing will be performed. Furthermore, the thread priorities of each thread on the search path after the priority level increase are retained. After the thread with the increased priority level releases the mutex it holds, its thread priority is restored to its pre-adjustment priority.
[0076] The thread control method provided by the embodiment of the present application is as follows: when the first thread is waiting to hold the first target lock, and the first target lock is a mutex lock, if the thread priority of the second thread currently holding the first target lock is lower than the second priority, the thread priority of the second thread is increased. In view of the fact that the second thread may be in a blocked state due to waiting to hold other mutex locks in the mutex lock mechanism, when the second target lock is waiting to hold the second target lock, the third thread holding the second target lock is similarly determined, and if the thread priority of the third thread is lower than the second priority, the thread priority of the third thread is increased. This can speed up the running speed of all related threads in the process of releasing the first target lock, thereby speeding up the release process of the first target lock and reducing the waiting time of the first thread waiting to hold the first target lock.
[0077] See also Figure 5, which shows a structural block diagram of a thread control device 400 provided in an embodiment of the present application. The thread control device 400 applies the above-mentioned electronic device, and the thread control device 400 includes: a thread determination module 410 and a priority adjustment module 420. The thread determination module 410 is used to determine the second thread currently holding the first target lock when the first thread is waiting to hold the first target lock, wherein the thread priority of the first thread is higher than the first priority; the priority adjustment module 420 is used to increase the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, and the second priority is lower than the first priority.
[0078] In some embodiments, the priority adjustment module 420 can be specifically used to: if the thread priority of the second thread is lower than the second priority, determine the remaining resources of the current electronic device; if the remaining resources are more than the first preset resources, increase the thread priority of the second thread to the thread priority of the first thread.
[0079] In a possible embodiment, the priority adjustment module 420 can also be used to: if the remaining resources are less than or equal to the first preset resources, and the remaining resources are more than the second preset resources, then the thread priority of the second thread is increased to a third priority, the second preset resources are less than the first preset resources, the third priority is lower than the thread priority of the first thread, and the third priority is higher than the thread priority of the second thread before adjustment.
[0080] In some embodiments, the first target lock is a read-write lock, and the thread control device 400 may further include a thread recording module. The thread recording module is configured to record all second threads currently holding the first target lock in a preset queue. In this manner, the thread determination module 410 may be specifically configured to: when a first thread is waiting to hold the first target lock, determine the second thread currently holding the first target lock based on the preset queue.
[0081] In one possible embodiment, the priority adjustment module 420 can be specifically used to: if the thread priority of the second thread is lower than the second priority, and the number of the second threads in the preset queue is greater than the preset number, then increase the thread priority of the target thread in the preset queue, and the target thread is part of the second threads in the preset queue.
[0082] Optionally, the priority adjustment module 420 can also be specifically used to: if the thread priority of the second thread is lower than the second priority, and the number of the second threads in the preset queue is greater than a preset number, obtain the preset number of second threads from the preset queue as the target threads according to the thread priority of each second thread, wherein the thread priority of the target thread is higher than the thread priority of other threads in the preset queue except the target thread; and increase the thread priority of the target thread in the preset queue.
[0083] In one possible implementation, the thread control device 400 may further include a first releasing module. The first releasing module is configured to, after increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, restore the thread priority of the second thread to the thread priority before adjustment if the second thread releases the critical section corresponding to the first target lock.
[0084] In some embodiments, the first target lock is a mutex lock. The thread determination module 410 may further be configured to, after increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, determine a third thread currently holding the second target lock if the second thread is currently waiting to hold the second target lock; and the priority adjustment module 420 may further be configured to increase the thread priority of the third thread if the thread priority of the third thread is lower than the second priority.
[0085] In a possible implementation, the thread control device 400 may further include a second releasing module. The second releasing module is configured to, after increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, restore the thread priority of the second thread to the thread priority before adjustment if the second thread releases the first target lock.
[0086] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and modules can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0087] In several embodiments provided in this application, the coupling between modules may be electrical, mechanical or other forms of coupling.
[0088] In addition, the functional modules in the various embodiments of the present application may be integrated into a processing module, or each module may exist physically separately, or two or more modules may be integrated into a single module. The above-mentioned integrated modules may be implemented in the form of hardware or software functional modules.
[0089] In summary, the solution provided by this application is to determine the second thread currently holding the first target lock when the first thread is waiting to hold the first target lock, and the thread priority of the first thread is higher than the first priority. If the thread priority of the second thread is lower than the second priority, then the thread priority of the second thread is increased, and the second priority is lower than the first priority. In this way, when the first thread with a thread priority higher than the first priority is waiting to hold the lock, the thread priority of the second thread currently holding the lock with a thread priority lower than the second priority can be increased, thereby speeding up the operation of the second thread, allowing the first thread to hold the lock faster, and thus improving the running smoothness of the first thread.
[0090] Please refer to Figure 6 , which shows a structural block diagram of an electronic device provided in an embodiment of the present application. The electronic device 100 can be an electronic device capable of running applications, such as a smartphone, a tablet computer, a smart watch, an e-book, a laptop computer, etc. The electronic device 100 in the present application may include one or more of the following components: a processor 110, a memory 120, and one or more applications, wherein the one or more applications may be stored in the memory 120 and configured to be executed by one or more processors 110, and the one or more applications are configured to execute the method described in the aforementioned method embodiment.
[0091] The processor 110 may include one or more processing cores. The processor 110 utilizes various interfaces and circuits to connect various components within the electronic device 100. It executes instructions, programs, code sets, or instruction sets stored in the memory 120, and accesses data stored in the memory 120 to perform various functions and process data within the electronic device 100. Optionally, the processor 110 may be implemented using at least one of the following hardware forms: a digital signal processing (DSP), a field-programmable gate array (FPGA), or a programmable logic array (PLA). The processor 110 may integrate one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. The CPU primarily processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing display content; and the modem handles wireless communications. It is understood that the modem may not be integrated into the processor 110 and may be implemented separately via a communications chip.
[0092] The memory 120 may include a random access memory (RAM) or a read-only memory (ROM). The memory 120 may be used to store instructions, programs, codes, code sets, or instruction sets. The memory 120 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 implementing 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 also store data created by the electronic device 100 during use (such as a phone book, audio and video data, chat history data), etc.
[0093] Please refer to Figure 7 , which shows a block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable medium 800 stores program code, which can be called by a processor to execute the method described in the above method embodiment.
[0094] The computer-readable storage medium 800 can be an electronic memory such as a flash memory, an EEPROM (Electrically Erasable Programmable Read-Only Memory), an EPROM, a hard disk, or a ROM. Alternatively, the computer-readable storage medium 800 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 800 has storage space for program code 810 for executing any of the method steps described above. These program codes can be read from or written to one or more computer program products. The program code 810 can be compressed, for example, in a suitable form.
[0095] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A thread control method, characterized in that: Applied to electronic equipment, the method includes: When a first thread is waiting to hold a first target lock, determining a second thread currently holding the first target lock, wherein the thread priority of the first thread is higher than the first priority, and the first target lock includes a read-write lock, a mutex lock, a conditional lock, or a spin lock; If the thread priority of the second thread is lower than a second priority, the thread priority of the second thread is increased, and the second priority is lower than the first priority.
2. The method according to claim 1, characterized in that If the thread priority of the second thread is lower than the second priority, increasing the thread priority of the second thread includes: If the thread priority of the second thread is lower than the second priority, determining the remaining resources of the electronic device; If the remaining resources are greater than the first preset resources, the thread priority of the second thread is increased to the thread priority of the first thread.
3. The method according to claim 2, characterized in that If the thread priority of the second thread is lower than the second priority, increasing the thread priority of the second thread further includes: If the remaining resources are less than or equal to the first preset resources, and the remaining resources are more than the second preset resources, the thread priority of the second thread is increased to a third priority, the second preset resources are less than the first preset resources, the third priority is lower than the thread priority of the first thread, and the third priority is higher than the thread priority of the second thread before adjustment.
4. The method according to claim 1, wherein The first target lock is a read-write lock, and the method further includes: Recording all second threads currently holding the first target lock in a preset queue; The method of determining a second thread currently holding the first target lock when the first thread waits to hold the first target lock includes: When the first thread waits to hold the first target lock, a second thread currently holding the first target lock is determined based on the preset queue.
5. The method according to claim 4, characterized in that If the thread priority of the second thread is lower than the second priority, increasing the thread priority of the second thread includes: If the thread priority of the second thread is lower than the second priority, and the number of the second threads in the preset queue is greater than a preset number, the thread priority of the target thread in the preset queue is increased, and the target thread is part of the second threads in the preset queue.
6. The method according to claim 5, characterized in that If the thread priority of the second thread is lower than the second priority, and the number of the second threads in the preset queue is greater than a preset number, increasing the thread priority of the target thread in the preset queue includes: If the thread priority of the second thread is lower than the second priority, and the number of the second threads in the preset queue is greater than a preset number, obtaining the preset number of second threads from the preset queue as the target threads according to the thread priority of each second thread, wherein the thread priority of the target thread is higher than the thread priority of other threads in the preset queue except the target thread; Raise the thread priority of the target thread in the preset queue.
7. The method according to claim 4, characterized in that After increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, the method further includes: If the second thread releases the critical section corresponding to the first target lock, the thread priority of the second thread is restored to the thread priority before adjustment.
8. The method according to claim 1, characterized in that The first target lock is a mutual exclusion lock. After increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, the method further includes: If the second thread is currently waiting to hold a second target lock, determining a third thread currently holding the second target lock, where the second target lock is a mutex lock; If the thread priority of the third thread is lower than the second priority, the thread priority of the third thread is increased.
9. The method according to claim 8, characterized in that After increasing the thread priority of the second thread if the thread priority of the second thread is lower than the second priority, the method further includes: If the second thread releases the first target lock, the thread priority of the second thread is restored to the thread priority before adjustment.
10. A thread control device, characterized in that: The device includes: a thread determination module and a priority adjustment module, wherein: The thread determination module is configured to determine, when a first thread is waiting to hold a first target lock, a second thread currently holding the first target lock, wherein the thread priority of the first thread is higher than the first priority, and the first target lock includes a read-write lock, a mutex lock, a conditional lock, or a spin lock; The priority adjustment module is configured to increase the thread priority of the second thread if the thread priority of the second thread is lower than a second priority, where the second priority is lower than the first priority.
11. An electronic device, characterized in that: include: one or more processors; Memory; One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to perform the method according to any one of claims 1 to 9.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program code, which can be called by a processor to execute the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Multi-thread synchronization method, terminal and computer-readable storage medium
CN107133092A
Thread execution method and device, electronic equipment and storage medium
CN114691376A