CN117271144B - Thread processing method and electronic equipment - Google Patents

Thread processing method and electronic equipment Download PDF

Info

Publication number
CN117271144B
CN117271144B CN202311561678.2A CN202311561678A CN117271144B CN 117271144 B CN117271144 B CN 117271144B CN 202311561678 A CN202311561678 A CN 202311561678A CN 117271144 B CN117271144 B CN 117271144B
Authority
CN
China
Prior art keywords
thread
condition variable
wake
information
priority
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202311561678.2A
Other languages
Chinese (zh)
Other versions
CN117271144A (en
Inventor
金鑫
朱金鹏
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Honor Device Co Ltd
Original Assignee
Honor Device Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Honor Device Co Ltd filed Critical Honor Device Co Ltd
Priority to CN202311561678.2A priority Critical patent/CN117271144B/en
Publication of CN117271144A publication Critical patent/CN117271144A/en
Application granted granted Critical
Publication of CN117271144B publication Critical patent/CN117271144B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5044Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering hardware capabilities
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The application discloses a thread processing method and electronic equipment, relates to the technical field of computers, and is applied to the electronic equipment. Comprising the following steps: in the event that the first thread pauses execution of the first task by waiting for a target condition variable, the first thread Cheng Huoqu suspends thread information of the second thread. The second thread wakes up the target condition variable by executing the second task. Priority of the second thread of the first thread Cheng Disheng. The first thread continues to execute the first task while the second thread wakes up the target condition variable by executing the second task. In the application, the first thread can raise the priority of the second thread, so that the second thread can execute the corresponding second task preferentially, the occupation time of the second thread to execute the second task wake-up condition variable is shortened, the time of the first thread waiting for the wake-up condition variable is shortened, and the execution efficiency of the multithread is improved.

Description

Thread processing method and electronic equipment
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a thread processing method and electronic equipment of a multithreading processing scene.
Background
In order to meet the requirements of multiple functions of electronic devices, the development of the operating system kernel (Linux kernel) of the existing electronic device basically adopts a multithreading development technology. Due to limited resources available to a central processing unit (central processing unit, CPU) of an electronic device, multithreading has the problem that threads are blocked due to waiting for CPU resources to be acquired in the process of executing tasks.
Disclosure of Invention
The embodiment of the application provides a thread processing method and electronic equipment, wherein a first thread shortens the occupation time of executing a second task wakeup condition variable by a second thread by improving the priority of the second thread wakeup target condition variable, thereby shortening the wait time of the first thread waiting for the wakeup condition variable and improving the execution efficiency of multiple threads.
In order to achieve the above object, the following technical solution is adopted in the embodiments of the present application.
In a first aspect, a method for processing a thread is applied to an electronic device, where the electronic device includes a condition variable module and a plurality of threads, and the plurality of threads includes a first thread and a second thread, and the method includes:
in the event that the first thread pauses execution of the first task by waiting for the target condition variable to wake up, the first thread Cheng Huoqu suspends thread information for the second thread. The second thread wakes up the target condition variable by executing the second task.
The first thread Cheng Disheng is configured to continue executing the first task when the second thread wakes up the target condition variable by executing the second task.
In the application, when the first thread pauses to execute the first task due to the awakening of the wait condition variable, the first thread can acquire the thread information of the second thread of the awakening condition variable, so that the first thread can improve the priority of the second thread, the second thread can preferentially execute the corresponding second task as much as possible, the occupation time of the second thread to execute the second task awakening condition variable is shortened, the wait time of the first thread for awakening the wait condition variable is shortened, and the execution efficiency of multiple threads is improved.
In a possible implementation manner of the first aspect, the method further includes:
Before the second thread executes the second task, writing the thread information of the second thread corresponding to the target condition variable into the condition variable module;
Then, the thread information of the second thread of the first thread Cheng Huoqu includes:
The first thread acquires the thread information of the second thread corresponding to the target condition variable from the condition variable module.
In the application, the first thread can acquire the thread information of the second thread corresponding to the target condition variable from the condition variable module, and timely acquire the wake-up source of the target condition variable, so that priority lifting operation can be performed on the wake-up source, the time for the wake-up source to execute tasks to wake up the target condition variable is shortened, the time for the first thread to wait for the target condition variable to be awakened is shortened, and the operating efficiency of multiple threads is improved.
In another possible implementation manner of the first aspect, the plurality of threads includes a third thread, and the third thread is a thread that wakes up the target condition variable by executing a third task; the thread information of the second thread of the first thread Cheng Huoqu includes:
the first thread determines one thread as a second thread from the plurality of third threads based on the thread information of each third thread, and acquires the thread information of the second thread.
In the application, the plurality of third threads are all threads capable of waking up the target condition variable, the second thread is determined from the plurality of third threads, the determined second thread is the most likely thread actually waking up the target condition variable, and the accuracy of the second thread is higher based on the actual running condition. The priority of the second thread of the first thread Cheng Disheng can truly reduce the time for the first thread to wait for the target condition variable to be awakened, and improve the running efficiency of the multithreading.
In another possible implementation manner of the first aspect, the thread information includes a number of wake-up times of each third thread wake-up target condition variable and/or an occupied duration of each third thread Cheng Zhihang third task wake-up target condition variable.
The first thread determines one thread as a second thread from a plurality of third threads based on thread information of each third thread, and the method comprises the following steps:
When the thread information comprises the awakening times, the first thread determines one third thread with the highest awakening times as a second thread based on the awakening times of all the third threads;
Or when the thread information comprises occupied time length, the first thread determines one third thread with the longest occupied time length as a second thread based on the occupied time length of each third thread;
Or when the thread information comprises the awakening times and the occupied time, the first thread determines one third thread as a second thread based on the awakening times and the occupied time of each third thread; the more the number of awakening times of one third thread, the greater the probability that one third thread is the second thread; the longer the duration of occupation of a third thread, the greater the probability that a third thread is a second thread.
In the present application, the thread information of the plurality of third threads may be stored in the memory or the condition variable module. The first thread determines a second thread from a plurality of third threads based on the thread information of the third threads, the determined second thread is most likely to be the thread actually waking up the target condition variable, and the accuracy of the second thread is higher based on the actual running condition. The priority of the second thread of the first thread Cheng Disheng can truly reduce the time for the first thread to wait for the target condition variable to be awakened, and improve the running efficiency of the multithreading.
In another possible implementation manner of the first aspect, the condition variable module includes a thread table, and the thread table includes at least one third thread; the method further comprises the steps of:
after the target condition variable is awakened, the first thread acquires thread information of a fourth thread awakening the target condition variable; the first thread adds the thread information of the fourth thread to the thread table.
Then, the first thread determines, based on the thread information of each third thread, one thread to be the second thread from the plurality of third threads, including:
The first thread determines one thread as a second thread from the plurality of third threads based on thread information of each third thread in the thread table.
In the application, the thread information of the third thread in the thread table can be dynamically updated in the process of executing tasks by each thread, and when the thread information of the fourth thread of the wake-up target condition variable is obtained each time, the thread information of the fourth thread is added into the thread table, and the thread information of each condition variable stored in the thread table is the information closest to the actual situation, so that the accuracy of the second thread is improved.
In another possible implementation manner of the first aspect, the thread information includes a wake-up number of wake-up target condition variables of each third thread, and each thread in the thread table is ordered from more to less according to the wake-up number; the first thread adds the thread information of the fourth thread to the thread table, including:
if the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread creates a new third thread in the thread table based on the thread information of the fourth thread according to the ordering of the wake-up times of the fourth thread in the thread table;
If the target third thread matched with the fourth thread exists in the thread table, the first thread increases the awakening frequency of the target third thread by 1.
In the application, the first thread can update the thread information corresponding to the target condition variable in the thread table based on the thread of the actual awakening target condition variable in the process of awakening the target condition variable for a plurality of times, so that the thread information of each condition variable stored in the thread table is the information closest to the actual condition, and the accuracy of the second thread is improved.
In another possible implementation manner of the first aspect, the thread information includes an occupied duration of a third task wakeup target condition variable of each third thread Cheng Zhihang, and each thread in the thread table is ordered from long to short according to the occupied duration; the first thread adds the thread information of the fourth thread to the thread table, including:
If the third thread which is connected with the fourth thread Cheng Pipei does not exist in the thread table, the first thread creates a new third thread in the thread table based on the thread information of the fourth thread according to the ordering of the occupied duration of the fourth thread in the thread table;
if a target third thread matched with the fourth thread exists in the thread table, the first thread updates the occupied duration of the target third thread, and the updated occupied duration of the target third thread is the sum of the occupied duration of the target third thread before updating and the occupied duration of the fourth thread.
In the application, the first thread can update the thread information corresponding to the target condition variable in the thread table based on the thread of the actual awakening target condition variable in the process of awakening the target condition variable for a plurality of times, so that the thread information of each condition variable stored in the thread table is the information closest to the actual condition, and the accuracy of the second thread is improved.
In another possible implementation manner of the first aspect, the plurality of threads includes a fifth thread, the fifth thread including a thread that is executing a task and/or a thread that waits for executing a task in addition to the second thread, a priority of the fifth thread being higher than or equal to an initial priority of the second thread; priority of the second thread of the first thread Cheng Disheng, comprising:
the first thread determines the target priority of the second thread based on the priorities of the fifth threads;
the first thread increases the initial priority of the second thread to the target priority.
According to the method and the device, the first thread can determine the target priority of the second thread based on the priorities of the plurality of fifth threads, so that normal operation of other threads is prevented from being influenced while the priority of the second thread is improved, the priority of the second thread is improved while influence of the second thread on the multithreading operation environment is reduced, the probability that the second thread preferentially obtains CPU resources to execute the second task is improved, and the time for the second thread to execute the second task to wake up target condition variables is shortened as much as possible.
In another possible implementation manner of the first aspect, after the first thread continues to execute the first task, the method further includes:
The first thread adjusts the priority of the second thread to the initial priority.
In the application, the first thread adjusts the priority of the second thread back to the initial priority, so that the influence of the priority of the second thread on the normal operation of other threads in a multithreading environment can be avoided.
In a second aspect, an electronic device is provided that includes a memory, a display screen, and one or more processors; the memory, the display screen and the processor are coupled; the memory has stored therein computer program code comprising computer instructions which, when executed by the processor, cause the electronic device to perform the method of any of the first aspects described above.
In a third aspect, there is provided a computer readable storage medium having instructions stored therein which, when run on an electronic device, cause the electronic device to perform the method of any of the first aspects described above.
In a fourth aspect, there is provided a computer program product comprising instructions which, when run on an electronic device, cause the electronic device to perform the method of any of the first aspects above.
In a fifth aspect, an embodiment of the application provides a chip comprising a processor for invoking a computer program in memory to perform a method as in the first aspect.
It will be appreciated that the advantages achieved by the electronic device according to the second aspect, the computer readable storage medium according to the third aspect, the computer program product according to the fourth aspect, and the chip according to the fifth aspect provided above may refer to the advantages in any one of the possible designs of the first aspect and the second aspect, and will not be described herein again.
Drawings
FIG. 1 is a schematic diagram of a multi-threaded execution awakened based on a condition variable according to an embodiment of the present application;
FIG. 2 is a schematic diagram of a thread with a wait condition variable awakened according to an embodiment of the present application;
Fig. 3 is a block diagram of an electronic device according to an embodiment of the present application;
FIG. 4 is an interactive flow diagram of a thread processing method according to an embodiment of the present application;
FIG. 5 is an interactive flow chart of another thread processing method according to an embodiment of the present application;
FIG. 6 is an interactive flow chart of another thread processing method according to an embodiment of the present application;
FIG. 7 is an interactive flow chart of another thread processing method according to an embodiment of the present application;
FIG. 8 is a schematic diagram of a thread table update according to an embodiment of the present application;
FIG. 9 is a schematic diagram of another thread table update provided in an embodiment of the present application;
FIG. 10 is a schematic diagram of another thread table update provided in an embodiment of the present application;
FIG. 11 is a schematic diagram illustrating another embodiment of a thread table update.
Detailed Description
In the description of embodiments of the present application, the terminology used in the embodiments below is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. As used in the specification of the present application and the appended claims, the singular forms "a," "an," "the," and "the" are intended to include, for example, "one or more" such forms of expression, unless the context clearly indicates to the contrary. It should also be understood that in the following embodiments of the present application, "at least one", "one or more" means one or more than two (including two). The term "and/or" is used to describe an association relationship of associated objects, meaning that there may be three relationships; for example, a and/or B may represent: a alone, a and B together, and B alone, wherein A, B may be singular or plural. The character "/" generally indicates that the context-dependent object is an "or" relationship.
Reference in the specification to "one embodiment" or "some embodiments" or the like means that a particular feature, structure, or characteristic described in connection with the embodiment is included in one or more embodiments of the application. Thus, appearances of the phrases "in one embodiment," "in some embodiments," "in other embodiments," and the like in the specification are not necessarily all referring to the same embodiment, but mean "one or more but not all embodiments" unless expressly specified otherwise. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless expressly specified otherwise. The term "coupled" includes both direct and indirect connections, unless stated otherwise. The terms "first," "second," and the like are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated.
In embodiments of the application, words such as "exemplary" or "such as" are used to mean serving as an example, instance, or illustration. Any embodiment or design described herein as "exemplary" or "e.g." in an embodiment should not be taken as preferred or advantageous over other embodiments or designs. Rather, the use of words such as "exemplary" or "such as" is intended to present related concepts in a concrete fashion.
In order to meet the requirements of multiple functions of electronic devices, the development of the operating system kernel (Linux kernel) of the existing electronic device basically adopts a multithreading development technology. Multithreading is the application of the principle of concurrent execution mechanism in an operating system in a program, and divides the program into a plurality of subtasks, and the subtasks are executed concurrently, and each task is a thread.
The thread execution tasks require resource scheduling, i.e., obtaining usage rights of the central processing unit (central processing unit, CPU). In the scenario of multithreading execution, the CPU resource scheduling of each thread may be performed based on a time-sharing scheduling mechanism. For example, all threads use the CPU in turn, and the time (CPU resource) occupied by each thread is equally allocated. Or in the scenario of multi-thread execution, the CPU resource allocation of each thread may be performed based on the priority of each thread. For example, a thread with a high priority is preferentially allowed to use the CPU, and if the priorities of the threads are the same, one thread is randomly selected to preferentially use the CPU, so that the time (CPU resource) occupied by the CPU is preferentially obtained.
The multithreading can improve the running efficiency of the program and make the utilization rate of the CPU higher. However, for one core of the CPU, only one thread can be executed at a time. Although with the development of technology, a core processor is developed into a multi-core processor, the resources that can be provided by a multi-core CPU are limited, and in the process of executing tasks, some threads have the problem of thread blocking caused by waiting for obtaining the CPU resources.
For example, in a scenario of allocating CPU resources of each thread based on priority, a thread a with a low priority performs task 1 first, a thread B with a high priority performs task 2 later, during actual task execution, a thread B with a high priority may preempt CPU resources of a thread a with a low priority, and a thread B preempts CPU resources to perform task 2, so that thread a is forced to suspend executing task 1, and needs to wait for obtaining CPU resources again to continue to perform task 1.
With the development of multithreading technology, the occurrence of mechanisms such as mutual exclusion lock and conditional variable are used for solving various problems such as thread blocking and thread synchronization in a multithreading scene.
The mutual exclusion lock realizes a simple form of mutual exclusion (mutual exclusion) synchronization, and is a common thread synchronization mechanism for protecting mutual exclusion access of shared resources in a multithreading environment. The mutex lock enables only one thread to access a shared resource (e.g., a CPU resource) at the same time, and other threads need to wait. For example, thread A may be performing a mutex lock on other threads such that the other threads are waiting to be unlocked by the mutex lock. Only thread a may use CPU resources at the same time. After the thread A finishes executing the task, the mutual exclusion lock can be released, and other threads can enter a queue waiting for acquiring CPU resources after releasing the mutual exclusion lock.
The condition variable is a multi-threaded synchronization mechanism for communicating and coordinating between multiple threads. The condition variables allow one or more threads to continue execution after waiting for a certain condition to be met, thereby avoiding busy waiting of threads. The principle of the condition variable is based on a waiting queue mechanism, and when a thread executes to call a waiting function, the thread can add itself to the waiting queue of the condition variable. When the condition is met, the wake-up function will select one or more threads from the wait queue to wake them up. For example, when thread a waits for a condition variable to be awakened, thread a needs to suspend tasks while waiting, i.e., thread a may add itself to the wait queue for the condition variable. When other threads wake up the condition variable by executing other tasks, the other threads can send wake-up broadcast to all threads in the waiting queue of the condition variable, and the thread A receives the wake-up broadcast and continues to execute the corresponding tasks.
The execution state of the thread includes a new state, a ready state, an running state, a blocked state, and a dead state.
The new state refers to that when a thread is created by new, the thread does not start running yet, and at this time, the thread is in the new state. The thread in the newly created state has not yet started running.
A newly built thread does not run automatically, the thread is required to be executed, a thread start () method is required to be called manually, and after the start () method returns, the thread is in a ready state and waits for the dispatch of a processor.
After the thread acquires the time of the CPU (acquires the CPU resource), the thread enters an operation state, and truly executes the content in the run () method.
The blocking state may be entered during thread running for various reasons: for example, a thread calls sleep () to go to sleep (also a blocking state). Or the thread invokes an operation that is blocked on input/output (I/O) and is in a blocked state before the I/O operation is completed. Or the thread waits to be blocked while releasing the lock. Or the thread is blocked while waiting for other trigger conditions, such as the thread waiting for a condition variable to wake. The blocking state is that the running thread does not run to finish, but temporarily gives up the CPU resource, and waits for the next CPU resource acquisition.
Thread death occurs for two reasons: the run () method ends normally; an uncaptured exception terminates the run () method causing sudden thread death.
The embodiment of the application is introduced for the situation that the thread enters the blocking state.
In some scenarios, a thread may enter a blocked state because of being locked with a mutex. For example, after thread A is mutex locked, the blocking state is entered from the running state. If the thread B with the mutex lock is preempted by other threads, the CPU resource of the thread B is occupied and cannot execute the corresponding task, so that the mutex lock cannot be released in time, and the thread A with the mutex lock is always in a blocking state.
In some scenarios, a thread may enter a blocked state because of a wait condition variable. For example, thread a enters the blocked state from the running state because a certain condition is not met, either actively blocked, or waiting for a condition variable to wake up. The thread B of the wake-up condition variable may be preempted by other threads, so that the CPU resource of the thread B is occupied and cannot execute the corresponding task to wake up the condition variable, and the thread A waiting for the wake-up of the condition variable is always in a blocking state.
This embodiment is described in connection with FIG. 1 for a scenario in which a thread wait condition variable is awakened. Referring to fig. 1, fig. 1 shows a schematic diagram of a multi-threaded execution based on a conditional variable wakeup. In fig. 1, thread a is a thread whose wait condition variable is awakened, and the priority of thread a is high. When the thread A waits for the condition variable to be awakened, the thread B awakening the condition variable obtains CPU resources and executes the corresponding task. Ideally, thread B should complete its corresponding task, wake up the condition variable, wake up thread a so that thread a can continue to perform its corresponding task. However, in actual situations, since the priority of the thread B is low, there may be a case where the CPU resource is preempted by the thread C with medium priority during the task execution of the thread B. The medium priority thread C preempts CPU resources and preferentially executes corresponding tasks, so that the thread B cannot execute the corresponding tasks at a later time. The thread B needs to wait for the thread C to finish the task to release the CPU resource, so that the CPU resource can be acquired to execute the corresponding task to wake up the condition variable. And after the thread B finishes the task, the thread B wakes up the condition variable, sends a wake-up broadcast, and the thread A receives the wake-up broadcast and continues to execute the corresponding task.
In the process that the thread A waits for the condition variable to be awakened, the thread B of the awakening condition variable is preempted by the thread C with the medium priority to cause the CPU resource to be blocked, so that the thread C with the medium priority is executed before the thread A with the high priority, the time for the thread A with the high priority to wait for the condition variable to be awakened is longer, and the execution efficiency of the thread A and the thread B is reduced.
Illustratively, FIG. 2 presents a schematic view of a thread waiting for a condition variable to be awakened. The execution state of thread a and the execution state of thread B are included in fig. 2. Wherein the shadow part is a thread running state, and the blank part is a thread blocking state. When the waiting condition variable of the thread A is awakened, the process of executing the task by the thread B may have the CPU resource preempted by at least one other thread with higher priority than the priority of the thread B, so that the process of executing the task by the thread B comprises a plurality of stages in a thread running state and a plurality of stages in a thread blocking state, the duration of executing the task by the thread B is prolonged, and the duration of awakening the waiting condition variable of the thread A is prolonged.
In order to reduce the time length of the waiting condition variable of the thread A with high priority when being awakened, the execution efficiency of the multithreading is improved, and under the scene that the thread A is locked with the mutual exclusion lock, the thread B can be enabled to execute the corresponding task preferentially by lifting the thread B locked with the mutual exclusion lock, so that the mutual exclusion lock is released timely, and the thread A enters the running state timely to continue executing the task. Or under the scene that the thread A waits for the condition variable to be awakened, the priority of the thread B of the awakening condition variable is increased, so that the thread B preferentially executes the corresponding task, thereby awakening the condition variable in time, and enabling the thread A to enter the running state in time to continue executing the task.
In the mutex lock scenario, thread B has a lock operation before thread A is mutex locked by thread B. Thus, the blocking source of thread A is known, and for thread B with a lock operation, a priority boost operation may be performed directly on thread B.
In the scenario where the condition variable is different from the mutex lock and the thread a waits for the condition variable to be awakened, the condition variable may be awakened by the thread B or the thread C. When thread B or thread C wakes up a condition variable, thread B or thread C may send a wake broadcast to all threads. Since thread a receives the wake-up broadcast, it is unknown to whom the wake-up source is at all.
The embodiment of the application provides a processing method of a thread for a scene that a thread waiting condition variable is awakened, when a first thread pauses execution of the first task due to the awakening of the waiting condition variable, the first thread can acquire thread information of a second thread of the awakening condition variable, so that the first thread can improve the priority of the second thread, the second thread can preferentially execute the corresponding second task as much as possible, the occupation time of the second thread to execute the second task awakening condition variable is shortened, the awakening waiting time of the first thread waiting condition variable is shortened, and the multithreading execution efficiency is improved.
The thread processing method provided by the embodiment of the application can be applied to electronic equipment. The electronic device in the embodiment of the present application may be a terminal device such as a portable computer (e.g. a mobile phone), a tablet computer, a notebook computer, a personal computer (personal computer, PC), a wearable electronic device (e.g. a smart watch), an augmented reality (augmented reality, AR) \virtual reality (VR) device, a vehicle-mounted computer, or may be a device such as a server, a server cluster, or the like, and the specific form of the electronic device is not limited in the following embodiments.
Exemplary, a schematic structural diagram of the electronic device 100 in fig. 3 is shown. Fig. 3 is a schematic hardware structure of an electronic device according to an embodiment of the present application. Referring to fig. 3, the electronic device shown in fig. 3 may include: processor 101, memory 102, communication interface 103, and bus 104. The processor 101, the memory 102, and the communication interface 103 may be connected via a bus 104.
The processor 101 is a control center of the electronic device, and may be a general-purpose central processing unit (central processing unit, CPU), or may be another general-purpose processor. Wherein the general purpose processor may be a microprocessor or any conventional processor or the like.
As an example, the processor 101 may include one or more CPUs, such as CPU0 and CPU1 shown in fig. 3.
In this embodiment, the processor 101 may allocate CPU resources for each thread according to the priority of the thread; or the processor 101 may allocate the time (CPU resource) that each thread occupies the CPU on average, so that each thread obtains the CPU resource in turn to execute the task.
Memory 102 may be, but is not limited to, a read-only memory (ROM) or other type of static storage device that can store static information and instructions, a random access memory (random access memory, RAM) or other type of dynamic storage device that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), magnetic disk storage or other magnetic storage device, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and that can be accessed by a computer.
In one possible implementation, the memory 102 may exist independently of the processor 101. The memory 102 may be coupled to the processor 101 through the bus 104 for storing data, instructions, or program code. When the processor 101 invokes and executes the instructions or the program codes stored in the memory 102, the split screen display method provided by the embodiment of the present application can be implemented.
In another possible implementation, the memory 102 may also be integrated with the processor 101.
A communication interface 103 for connecting the electronic device with other devices via a communication network, which may be an ethernet, a radio access network (radio access network, RAN), a wireless local area network (wireless local area networks, WLAN), etc. The communication interface 103 may include a receiving unit for receiving data and a transmitting unit for transmitting data.
Bus 104 may be an industry standard architecture (industry standard architecture, ISA) bus, an external device interconnect (PERIPHERAL COMPONENT INTERCONNECT, PCI) bus, or an extended industry standard architecture (extended industry standard architecture, EISA) bus, among others. The bus may be classified as an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown in fig. 3, but not only one bus or one type of bus.
It should be noted that the structure shown in fig. 3 does not constitute a limitation of the electronic device, and the electronic device may include more or less components than those shown in fig. 3, or may combine some components, or may be arranged with different components.
In some embodiments, an electronic device includes a condition variable module and a plurality of threads, including a first thread and a second thread. Wherein the condition variable module may include a plurality of condition variables, one of which may be awakened by one or more threads. Referring to fig. 4, a method for processing a thread according to this embodiment includes:
S201, a first line Cheng Zhihang is used for first tasks.
In this embodiment, the first task is a main task of the first thread. For example, the first thread may be a drawing thread, and then the first task may be a drawing task. For example, the first thread may be a rendering thread, and then the first task may be a rendering task. For example, the first thread may be a composite thread, then the first task may be a composite task, and so on.
S202, the first thread pauses to execute the first task and waits for the target condition variable to be awakened.
In this embodiment, during the process of the first task of the first thread Cheng Zhihang, when the first thread is executed to a certain stage or the execution state of the first thread does not meet the requirement of the target condition variable, the first thread pauses to execute the first task, enters a waiting state, and waits for the target condition variable to be awakened.
In some embodiments, the first thread may add itself to the wait queue corresponding to the target condition variable, and the thread in the wait queue may be considered a thread waiting for the target condition variable to be awakened. When the target condition variable is awakened, an awaken notification may be sent to one or more threads in the wait queue to awaken the respective thread to continue executing its respective task.
S203, the second thread executes a second task.
The second thread is a thread for waking up the target condition variable by executing a second task; accordingly, the second task is a required execution task for the second thread to wake up the target condition variable. That is, the target condition variable may be awakened when the second thread has completed executing the second task.
In this embodiment, the second task executed by the second thread and the first task executed by the first thread Cheng Zhihang may be executed asynchronously, and the order in which the first task executed by the first thread Cheng Zhihang and the second task executed by the second thread are not limited.
In some embodiments, the first thread acts as the main thread and the second thread acts as the thread that wakes up the target condition variable, the first thread often having a higher priority than the second thread. The order of the first tasks of the first line Cheng Zhihang may be prioritized over the execution of the second tasks of the second thread.
In some embodiments, the second thread may execute its corresponding primary task before executing the second task. For example, the second thread is a garbage collection thread, and the garbage collection thread may execute a main task such as garbage marking before executing the second task that wakes up the target condition variable.
S204, thread information of a second thread of the first thread Cheng Huoqu.
In this embodiment, the first thread may acquire the thread information of the second thread that wakes up the target condition variable. By way of example, the thread information may include a thread identification of the thread, e.g., the thread identification may include a thread number, a thread name, etc. The thread information may also include an identification of the conditional variable that the thread corresponds to waking up, e.g., the identification of the conditional variable may be a conditional variable name. The thread information may also include information such as the duration of the thread wake-up condition variable, the number of wake-up times, the wake-up time interval, etc. The occupied time length refers to the time length occupied by the thread from the start of executing the task to the completion of the task wakeup condition variable; the number of awakenings refers to the number of awakening a condition variable by a thread since the thread was running; the wake-up time interval refers to the time interval between wake-up time of each two adjacent wake-up condition variables of the thread, and the wake-up time refers to the time when the thread completes the task wake-up condition variables.
The memory of the electronic device may be preset with each condition variable and thread information of a thread waking up each condition variable, so that the first thread may obtain thread information of a second thread corresponding to the target condition variable from the memory.
Or the condition variable module can preset each condition variable and wake up the thread information of the thread of each condition variable, so that the first thread can acquire the thread information of the second thread corresponding to the target condition variable from the condition variable module.
Or dynamically generating each condition variable and waking up the thread information of each thread of the condition variable in the running process of each thread, and storing the thread information in a memory or a condition variable module, so that the first thread can acquire the thread information of a second thread corresponding to the target condition variable from the memory or the condition variable module.
S205, priority of the second thread of the first thread Cheng Disheng.
In this embodiment, the priority of the thread means the priority of the thread to acquire the CPU resource. The higher the priority of a thread, the greater the probability that the thread will acquire CPU resources. In other words, when the CPU allocates resources for each thread, the CPU tends to allocate resources preferentially to the thread with high priority, so that the thread that preferentially obtains the CPU resources can execute tasks in time.
In some embodiments, the first thread may obtain a thread identification of the second thread, and set a priority of the second thread to a higher priority through an interface within the virtual machine based on the thread identification of the second thread, such that the second thread may wake up the target condition variable by preferentially executing the second task as much as possible. Or in some embodiments, the first thread may obtain the thread identifier of the second thread, send the thread identifier of the second thread to the CPU core, and set the priority of the second thread to a higher priority by the CPU core, so that the second thread may execute the second task as preferentially as possible to wake up the target condition variable.
The priority of the second thread is set by the interface in the virtual machine, and the range of the priority of the thread is limited. For example, the priority of the second thread is 124, and the highest priority of the second thread can be set to 100 by setting the priority of the second thread through an interface in the virtual machine, and the higher the number, the lower the priority. While the priority of the second thread may be set to a higher level by the way the CPU core sets the priority of the second thread, e.g., the priority of the second thread is set to be highest to 80. The way in which the priority of the second thread of the first thread Cheng Disheng may be determined according to the actual situation, which is not limited in this embodiment.
In some embodiments, the second thread priority may be raised directly to a preset target priority. For example, the priority of the second thread is 124, and the preset target priority corresponding to the second thread is 100, then the first thread may set the priority of the second thread through an interface in the virtual machine based on the thread identifier and the target priority of the second thread; or the first thread may send the thread identifier and the target priority of the second thread to the CPU core, so that the CPU core sets the priority of the second thread according to the thread identifier and the target priority of the second thread.
In some embodiments, multiple condition variables may be included in the context of multi-threaded execution, where there is a difference in the initial priority of threads waking up each condition variable, and where the priority of a thread is raised to a level that also requires consideration of the priority of other running threads. In order to make the target priority of each thread more fit with the running environment of the actual multithreading without affecting the original running conditions of other important threads, the first thread can dynamically acquire the target priority of the second thread, so as to perform the operation of lifting the priority of the second thread.
In some embodiments, the plurality of threads includes a fifth thread that includes threads that are executing tasks and/or threads waiting to execute tasks in addition to the second thread. In this embodiment, the priority of the fifth thread is higher than or equal to the initial priority of the second thread. The fifth thread is a thread currently occupying CPU resources or waiting for obtaining CPU resources, and whether the normal operation of the fifth thread is affected needs to be considered when the priority of the second thread is increased, so that the target priority of the second thread can be determined based on the fifth thread.
Then, in some embodiments, the priority of the first thread Cheng Disheng second thread includes:
First, the first thread determines a target priority of the second thread based on the priorities of the fifth threads.
The first thread may obtain the priority of each fifth thread, and determine the target priority of the second thread based on the priority of each fifth thread and the importance of the task performed by the fifth thread.
For example, in some embodiments, the priority of threads may be categorized as high priority, medium priority, and low priority. The higher the priority, the higher the importance of the task that the thread performs. If there is at least one low priority among the plurality of fifth threads, the target priority of the second thread may be higher than the low priority and lower than the medium priority, i.e., the priority of the second thread may be set to be higher than the highest priority among the low priorities and lower than the target priority of the lowest priority among the medium priorities.
In some embodiments, the priority may be represented by a natural number greater than 0. For example, the second thread has an initial priority of 124. In the multithreading operation environment, 5 fifth threads exist, and the priorities corresponding to the 5 fifth threads are respectively 10 for thread 1, 30 for thread 2, 50 for thread 3, 88 for thread 4 and 101 for thread 5.
Of these, by way of example, thread 1, thread 2, and thread 3 may be considered high priority threads having a priority of less than 50, and the task it performs is also an important task. Thread 4 is a medium priority thread with a priority of less than 100 and greater than 50 and performs tasks that are secondary tasks. Thread 5 is a low priority thread with a priority greater than 100 and performs the next-second task. The second thread's priority belongs to a low priority, and the 5 fifth threads each have a priority greater than the second thread's priority 124.
The first thread may determine that the target priority of the second thread is any priority between 89 and 100, based on the priorities of the respective fifth threads and the importance of executing tasks thereof, in order to avoid affecting the operation of the high priority thread and the medium priority thread, such that the second thread has a priority lower than the medium priority of thread 4, higher than the low priority of thread 5, and is executed in preference to thread 5.
It should be noted that, the target priority of the second thread may be higher than the medium priority, and in special cases, the target priority of the second thread may also be higher than the high priority, and the setting range of the target priority of the second thread may be determined according to the actual situation.
In some embodiments, there may be no low priority thread in the plurality of fifth threads, including medium priority threads and high priority threads. In this case, the first thread may determine the target priority of the second thread based on the thread proportion.
Illustratively, the second thread has an initial priority of 124. In the multithreading running environment, 10 fifth threads exist, and the 10 fifth threads correspond to 10 priority levels, namely, 10 priority levels corresponding to thread 1, 15 priority levels corresponding to thread 2, 30 priority levels corresponding to thread 3, 40 priority levels corresponding to thread 4, 50 priority levels corresponding to thread 5, 55 priority levels corresponding to thread 6, 60 priority levels corresponding to thread 7, 77 priority levels corresponding to thread 8, 88 priority levels corresponding to thread 9 and 98 priority levels corresponding to thread 10.
Wherein thread 1, thread 2, thread 3, thread 4 and thread 5 are high priority threads having a priority of less than or equal to 50, and the task it performs is also an important task. Thread 6, thread 7, thread 8, thread 9, and thread 10 are medium priority threads having a priority of less than 100 and greater than 50, and the task they perform is a secondary task.
The priority of the second thread belongs to a low priority, e.g. the thread proportion is set to 10%, i.e. the target priority of the second thread may be higher than the 10% of the threads of the fifth thread which are ordered from low to high priority. Of the 10 fifth threads described above, the second thread may be higher than one fifth thread, thread 10, which has a priority of 98, in order of thread scale and priority from low to high. The target priority of the second thread needs to be lower than the 90% of the threads of the fifth thread which are ordered from low to high, the lowest priority thread of the 90% of threads being thread 9 and having a priority of 88. Thus, the target priority of the second thread may be any of priorities between 89 and 97, such that the second thread's priority is lower than the priority of thread 9, higher than the priority of thread 10, prior to execution by thread 10. It should be noted that the thread proportion may be determined according to practical situations, for example, the thread proportion may be a value greater than 0 and less than 1, such as 5%, 10%, 20%, and the like, which is not limited in this embodiment.
After the first thread determines the target priority of the second thread based on the priorities of the fifth threads, the first thread increases the initial priority of the second thread to the target priority.
In this embodiment, after the target priority of the second thread of the first thread Cheng Queding, the initial priority of the second thread may be increased to the target priority through the interface setting in the virtual machine; or the thread identification and the target priority of the second thread are sent to the CPU core, and the initial priority of the second thread is improved to the target priority through the CPU core.
In this embodiment, the first thread may raise the priority of the second thread through multiple ways, and may determine the target priority of the second thread through multiple ways, so that when the priority of the second thread is raised, the normal operation of other threads is avoided being affected, when the influence of the second thread on the multithreading operation environment is reduced, the priority of the second thread is raised, the probability that the second thread preferentially obtains the CPU resource to execute the second task is improved, and the time for the second thread to execute the second task to wake up the target condition variable is shortened as much as possible.
S206, the second thread wakes up the target condition variable.
In this embodiment, the second thread may preferentially obtain CPU resources over initial priorities based on the promoted target priorities, thereby executing the second task. After completing the second task, the second thread wakes up the target condition variable.
In some embodiments, the second thread may also send a wake-up broadcast after waking up the target condition variable, and the first thread wakes up after receiving the wake-up broadcast, thereby continuing to execute the first task.
In some embodiments, the first thread Cheng Re is in the wait queue for the target condition variable, and the second thread may also choose to send a wake broadcast to one or more threads in the wait queue that contain the first thread to wake those threads in the wait queue to continue executing their respective tasks.
In this embodiment, after the second thread sends the wake condition variable and sends the wake broadcast, the second thread may continue to execute its primary task.
S207, the first thread continues to execute the first task.
In this embodiment, the first thread continues to execute the first task after receiving the wake-up broadcast.
In some embodiments, the processing method of the thread provided in this embodiment is described by taking the first thread as a drawing thread and taking the second thread as a garbage collection thread as an example. When executing a drawing task, the drawing thread executes the drawing task to a certain node, and needs to wait for a target condition variable to be awakened to temporarily execute the drawing task. At this time, the rendering thread may acquire thread information of the garbage collection thread corresponding to the target condition variable. And based on the thread information, the initial priority of the garbage collection thread is increased to the target priority. The garbage collection thread can obtain CPU resources to execute corresponding tasks preferentially due to the fact that the priority is improved. And after the garbage collection thread finishes the task, waking up the target condition variable and sending out a wake-up broadcast. The drawing thread continues to execute the drawing task after receiving the wake-up broadcast.
In the whole process, when the drawing thread waits for the target condition variable to be awakened, the thread which acquires the awakened target condition variable is the garbage collection thread, so that the priority of the garbage collection thread is improved, the garbage collection thread can execute the corresponding task preferentially and timely to awaken the target condition variable, the time from the execution of the task by the garbage collection thread to the awakening of the target condition variable is shortened, the time for the drawing thread to wait for the target condition variable to be awakened is shortened, and the problem that the drawing thread loses drawing image frames due to the fact that the drawing thread waits for a long time possibly occurs is avoided.
In this embodiment, when the first thread pauses executing the first task due to the wake-up of the wait condition variable, the first thread may acquire the thread information of the second thread of the wake-up condition variable, so that the first thread may raise the priority of the second thread, so that the second thread may preferentially execute the second task corresponding to the first thread as much as possible, thereby shortening the occupation time of the second thread to execute the wake-up condition variable of the second task, shortening the wait time of the first thread waiting for the wake-up of the condition variable, and improving the execution efficiency of the multithread.
In some embodiments, in the priority of the second thread of the first thread Cheng Disheng, so that the second thread may obtain the CPU resource preferentially to execute the second task, after waking up the target condition variable, the first thread may further adjust the priority of the second thread to the initial priority so as not to affect the normal operation of the other threads. Referring to fig. 5, fig. 5 illustrates another method of processing a thread, where after the first thread wakes up, the first thread may also execute:
S208, the first thread adjusts the priority of the second thread to the initial priority.
In this embodiment, the first thread may further include an initial priority of the second thread when acquiring the thread information of the second thread. The first thread may record the initial priority of the second thread, and after the first thread wakes up, adjust the priority of the second thread from the target priority back to the initial priority.
Similarly, the manner in which the first thread adjusts the priority of the second thread back to the initial priority is also referred to as S205, and the first thread may adjust the priority of the second thread back to the initial priority through an interface within the virtual machine. Or the first thread can also send the thread identification and the initial priority of the second thread to the CPU core, and the CPU core adjusts the priority of the second thread back to the initial priority. The way in which the first thread adjusts the priority of the second thread back to the initial priority may be determined according to practical situations, which is not limited in this embodiment.
In this embodiment, the first thread adjusts the priority of the second thread back to the initial priority, so that the influence of the priority of the second thread on the normal operation of other threads in the multithreading environment can be avoided.
In some embodiments, the first thread may be triggered by other threads to enter a state of waiting for the target condition variable to be awakened, and referring to fig. 6, fig. 6 shows another processing method of the thread, before the second thread executes the second task to awaken the target condition variable, further including:
s301, the second thread sets the value of the global variable corresponding to the target condition variable as a first value.
When the value of the global variable corresponding to the target condition variable is the first value, the thread related to the global variable can be triggered to enter a state of waiting for the target condition variable to be awakened. Wherein the thread associated with the global variable comprises a first thread. Illustratively, the global variable may be a boolean value and the first value may be false.
In this embodiment, the first thread may identify the value of the global variable corresponding to the target condition variable in real time during the task execution, and if it is identified that the value of the global variable becomes false, the execution of the first thread is suspended, and the state of waiting for the target condition variable to be awakened is entered (i.e. S202).
Accordingly, after the second thread wakes up the target condition variable in S206, the second thread may further update the value of the global variable to avoid triggering the thread related to the global variable to enter the waiting state by mistake, and referring to fig. 6, further includes:
S302, the second thread sets the value of the global variable corresponding to the target condition variable as a second value.
Illustratively, the global variable may be a boolean value and the second value may be true.
In some embodiments, the second thread sets the value of the global variable corresponding to the target condition variable to true, and then the thread associated with the global variable is not triggered to enter a state waiting for the target condition variable to be awakened.
The processing method of the thread provided in this embodiment is described by taking the first thread as a rendering thread and the second thread as a garbage collection thread as an example.
The garbage collection thread may set the global variable value of the target condition variable, weak_ref_access_enabled, to false during the process flag stack (process MARK STACKS AND REFERENCE) stage. If the thread related to the global variable detects that the value is false during the task execution, the thread enters a state of waiting for the target condition variable to be awakened.
After the garbage collection thread executes the corresponding task (second task), the value of the web_ref_access_enabled may be set back to true in parallel by waking up the target condition variables such as the web_ globals _add_condition_, the web_ intern _condition_, and the like. The second thread awakening target condition variable and the second thread modifying global variable can be synchronously executed or asynchronously executed, and the execution sequence is not limited during asynchronous execution.
In this embodiment, the second thread may trigger the thread related to the global variable to enter a state waiting for the target condition variable to be awakened by setting the value of the global variable corresponding to the target condition variable to be a first value, and the second thread may avoid triggering the thread related to the global variable to enter a state waiting for the target condition variable to be awakened by setting the value of the global variable corresponding to the target condition variable to be a second value, thereby avoiding a situation that the multithreading is disordered, and improving the efficiency of the multithreading.
In some embodiments, the information of the first thread Cheng Huoqu and the second thread may be obtained from a condition variable module storing a condition variable, in which case, the second thread may write its own information into the condition variable module for the first thread to obtain, referring to fig. 7, fig. 7 shows another processing method of the thread, before the second thread sets the value of the global variable corresponding to the target condition variable to the first value (S301), further including:
s401, the second thread writes the thread information of the second thread corresponding to the target condition variable into the condition variable module.
In this embodiment, by analyzing the execution flow of each thread, it is possible to determine to wake up one or more threads corresponding to each condition variable, and perform instrumentation before the thread executes a task corresponding to the wake-up condition variable, so that the thread information and the information of the corresponding condition variable are written into the condition variable module. For example, determining that the thread awakening the target condition variable is the second thread, and performing instrumentation before the second thread executes the second task, so that when the second thread runs to the instrumentation point, the thread information of the second thread corresponding to the target condition variable is written into the condition variable module.
In some embodiments, the thread information written into the condition variable module may include a thread identification, and may further include information such as an occupied duration of the thread executing task wake-up condition variable, a number of wake-up condition variable, and so on.
Then, the thread information of the second thread of the first thread Cheng Huoqu in S204 includes:
s2041, the first thread acquires thread information of a second thread corresponding to the target condition variable from the condition variable module.
In this embodiment, the second thread may write the thread information of the second thread corresponding to the target condition variable into the condition variable module before executing the second task, so that the first thread may directly obtain the thread information of the second thread corresponding to the target condition variable from the condition variable module when waiting for the target condition variable to be awakened, so as to improve the priority of the second thread.
In some embodiments, after the second thread wakes up the target condition variable, the thread information of the second thread corresponding to the target condition variable in the condition variable module may be further cleared, that is, the second thread performs after S206:
S402, the second thread clears the thread information of the second thread corresponding to the target condition variable in the condition variable module.
In this embodiment, after the second thread wakes up the target condition variable, the wake-up operation of the target condition variable is completed. In the scene of waking up the target condition variable next time, the second thread is possibly not a second thread of the target condition variable, so that the second thread can clear the thread information of the second thread corresponding to the target condition variable in the condition variable module, timely release the storage resource in the condition variable module and timely update the threads corresponding to the condition variables in the condition variable module.
In some embodiments, the processing method of the thread provided in this embodiment is exemplarily described by taking the first thread as a drawing thread and taking the second thread as a garbage collection thread as an example.
During the garbage collection thread execution garbage copy stage (CopyingPhase stage), the garbage collection thread may write the target condition variable weak_ intern and the thread information of the garbage collection thread into the condition variable module. The garbage collection thread may set the value of the global variable w eak ref access enable to false. When a thread (e.g., a rendering thread) associated with a global variable executes string. Interface, it is determined according to execution logic whether or not the leak_ref_access_enable is false. If false, wait for the target condition variable weak_ intern to wake up is needed. When the garbage collection thread completes the second task (e.g., garbage collection thread completes stage ProcessReferences), it wakes up the target condition variable weak_ intern and issues a wake-up broadcast. The first thread (drawing thread) continues to execute the first task after receiving the wake-up broadcast. After the garbage collection thread wakes up the target condition variable, the thread information of the garbage collection thread in the condition variable module can be emptied.
In an embodiment, the instrumentation may be performed before ProcessReferences stages of the garbage collection thread, so that the garbage collection thread writes the target condition variable and the thread information of the garbage collection thread into the condition variable module. When a first thread (for example, a drawing thread) is woken up by waiting for a target condition variable weak_ intern, the first thread can acquire thread information of a thread corresponding to the target condition variable from a condition variable module, and perform a priority lifting operation of the thread.
In some embodiments, the first thread may also obtain thread information for the second thread of the dynamically derived target condition variable. For example, the information of the second thread of the first thread Cheng Huoqu may be the thread information of determining that one thread is the second thread from the third threads of the wake target condition variables, and acquiring the thread information of the thread.
In some embodiments, the plurality of threads includes a third thread, the third thread being a thread that wakes up the target condition variable by executing the third task, the second thread being one of the third threads. The thread information of the second thread of the first thread Cheng Huoqu includes:
the first thread determines one thread as a second thread from the plurality of third threads based on the thread information of each third thread, and acquires the thread information of the second thread.
In this embodiment, the thread information may include, in addition to the thread identifier of each thread, information for characterizing the degree to which each thread is related to the corresponding condition variable.
In some embodiments, the thread information includes a number of wakeups for each third thread wake target condition variable and/or an occupancy time for each third thread Cheng Zhihang third task wake target condition variable. Then, the first thread determines, based on the thread information of each third thread, that one thread is a second thread from the plurality of third threads, including one of the following:
one of them is: when the thread information comprises the awakening times, the first thread determines one third thread with the highest awakening times as the second thread based on the awakening times of all the third threads.
In this embodiment, since the first thread cannot learn that the second thread actually wakes up the target condition variable before the target condition variable is waken up, the first thread may determine, from among the third threads that wake up the target condition variable, one third thread with the highest probability of waking up the target condition variable in the actual running process as the second thread corresponding to the target condition variable. When the thread information includes the number of wake-up times, the more the number of wake-up times, the greater the probability that the third thread is the wake-up target condition variable in the actual running process. The first thread may determine one third thread with the highest number of wake-ups from the plurality of third threads as the second thread.
The other is: when the thread information comprises occupied time length, the first thread determines one third thread with the longest occupied time length as a second thread based on the occupied time length of each third thread.
In this embodiment, when the thread information includes the occupied duration, the longer the occupied duration is, the more the number of times of the third thread waking up the target condition variable is shown to be accumulated, or the greater the possibility that the occupied duration is caused by the third thread being preempted in the process of waking up the target condition variable by the CPU resource; the greater the probability that the third thread is the wake-up target condition variable in the actual running process, the first thread can determine one third thread with the longest occupied duration from a plurality of third threads as the second thread.
Yet another is: when the thread information comprises the awakening times and the occupied time, the first thread determines one third thread as a second thread based on the awakening times and the occupied time of each third thread; the more the number of awakening times of one third thread, the greater the probability that one third thread is the second thread; the longer the duration of occupation of a third thread, the greater the probability that a third thread is a second thread.
In this embodiment, when the thread information includes the number of wake-ups and the occupied duration, the greater the number of wake-ups and the longer the occupied duration, the greater the probability of waking up the target condition variable in the actual running process of the third thread is indicated.
In some embodiments, an objective function may be set based on the number of wake-ups and the occupied duration of the thread, where the objective function is used to determine a third thread corresponding to the number of wake-ups and the occupied duration that is the longest, and the third thread is the second thread.
In some embodiments, a weighted value of the number of wake-up times and the occupied duration of the thread may be calculated, where the weighted coefficient of the number of wake-up times is w1, the weighted coefficient of the occupied duration is w2, and the weighted value of the number of wake-up times and the occupied duration is calculated. The larger the weighted value is, the larger the probability of waking up the target condition variable in the actual running process of the third thread is. Wherein the sum of w1 and w2 is 1, for example, w1 and w2 may both be 0.5; or w1 has a value of 0.4, w2 has a value of 0.6, etc. The values of w1 and w2 may be determined in accordance with the practice, and this is not limited in this embodiment.
In some embodiments, the thread information for the plurality of third threads may be stored in the memory or condition variable module in the form of a key value pair of "target condition variable-thread identification of third thread". The first thread determines a second thread from a plurality of third threads based on the thread information of the third threads, the determined second thread is most likely to be the thread actually waking up the target condition variable, and the accuracy of the second thread is higher based on the actual running condition. The priority of the second thread of the first thread Cheng Disheng can truly reduce the time for the first thread to wait for the target condition variable to be awakened, and improve the running efficiency of the multithreading.
In some embodiments, the thread information for the third plurality of threads may be stored in the condition variable module in the form of a thread table. The thread information updating process of the third thread in the thread table comprises the following steps:
after the target condition variable is awakened, the first thread acquires thread information of a fourth thread awakening the target condition variable; the first thread adds the thread information of the fourth thread to the thread table.
In some embodiments, the first thread waits for the target condition variable to be awakened. When the target condition variable is awakened and the first thread is awakened by receiving the awakening broadcast, the first thread can acquire the thread information of the fourth thread awakening the target condition variable. And updating the thread information of the fourth thread to the thread table corresponding to the target condition variable.
For example, the first thread may create a new third thread in the thread table directly based on the thread information of the fourth thread, i.e. the thread information of the fourth thread is written into the thread table to become one of the plurality of third threads in the thread table. Or the first thread may also determine whether a thread matching the fourth thread exists in the thread table, if so, update the thread information of the thread, and so on.
In some embodiments, the first thread may query the thread table based on the thread identification of the fourth thread, if there is a thread identification of the third thread that is consistent with the thread identification of the fourth thread. If so, the third and fourth threads Cheng Pipei are considered. The thread identifier may be a thread number or a thread name. The thread identification is unique.
In some embodiments, the thread table may be stored in memory or in a condition variable module. The thread table may record information such as thread identification (e.g., thread name), number of wake-up times, occupied time length, etc. of threads corresponding to each condition variable.
When the first thread waits for the target condition variable to be awakened, thread information of a second thread corresponding to the target condition variable can be obtained from the thread table.
In some embodiments, the thread information of the third thread in the thread table may be dynamically updated during the task execution of each thread, and the thread information of the fourth thread is added to the thread table each time the thread information of the fourth thread of the wake target condition variable is obtained.
In some embodiments, a thread table may be created based on the thread information for each thread to which each condition variable corresponds; a thread table may also be created for storing the respective condition variables and thread information for their corresponding threads. Creating a common thread table reduces the data redundancy of the created table, and this embodiment is described by taking as an example the creation of a thread table including each condition variable and thread information of the thread corresponding to each condition variable.
For example, in some embodiments, the thread information of each thread includes the number of wake-ups of the corresponding condition variable, and the threads corresponding to each condition variable in the thread table may be ordered according to the number of wake-ups from more to less, as shown in table 1 below:
TABLE 1
Table 1 includes a plurality of condition variables, for example, a condition variable 1, a condition variable 2, etc., and table 1 also includes a thread name and a wake-up number of threads B, a thread name and a wake-up number of threads C, etc. that can wake up the condition variable 1; the thread name and number of wakeups of thread D, thread E, etc. of condition variable 2 may be waked up.
The method for the first thread to acquire the thread information of the fourth thread and update the thread information into the thread table can comprise the following steps:
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread adds the thread information of the fourth thread to the thread table according to the order of the wake-up times of the fourth thread in the thread table.
For example, if the condition variable 1 is the target condition variable and the fourth thread is the thread F of the wake condition variable 1, the thread table (table 1) includes the thread B and the thread C of the wake target condition variable, and there is no thread matching the thread F. In this case, the number of wake-ups of the fourth thread of the first thread Cheng Huoqu, for example, the number of wake-ups of the fourth thread is 1, and then, according to the order of the wake-ups in the thread table, the number of wake-ups of the thread F is less than that of the thread C, and then, the first thread may insert the thread information of the fourth thread into the next list of the thread C. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
For example, if the condition variable 2 is the target condition variable and the fourth thread is the thread G of the wake condition variable 2, the thread table (table 1) includes the thread D and the thread E of the wake target condition variable, and there is no thread matching the thread G. In this case, the number of wake-ups of the fourth thread of the first thread Cheng Huoqu, for example, the number of wake-ups of the fourth thread is 1, and then, according to the ordering of the wake-ups of the threads corresponding to the condition variable 1 in the thread table, the wake-ups of the thread G is equal to the wake-ups of the existing threads D and E. In this case, the first thread may insert the thread information of the fourth thread into the last list corresponding to the target condition variable, that is, insert the thread information of the fourth thread into the next list of thread E. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
In some embodiments, if the condition variable is never awakened, the thread information corresponding to the condition variable in the thread table is null.
If the target third thread matched with the fourth thread exists in the thread table, the first thread increases the awakening frequency of the target third thread by 1.
In this embodiment, for example, if the condition variable 1 is the target condition variable and the fourth thread is the thread B of the wake condition variable 1, the thread table (table 1) includes the thread B and the thread C of the wake target condition variable, and there is a thread matching the thread B. In this case, the number of wake-ups of the fourth thread of the first thread Cheng Huoqu, for example, the number of wake-ups of the fourth thread is1, and the number of wake-ups of the third thread matched with the number of wake-ups is increased by 1. For example, the number of wake-up times of the thread B in the thread table is 3, and the number of wake-up times of the thread B in the thread table is updated to be 4 by increasing the number of wake-up times by 1.
In some embodiments, an exemplary thread table update diagram is presented in FIG. 8. Referring to fig. 8, when the thread a waits for the condition variable 1 to be awakened, thread information of the thread corresponding to the condition variable 1 in the thread may be acquired. If the condition variable 1 is never awakened, the thread information corresponding to the condition variable 1 in the thread table is empty. In this case, the thread a cannot acquire the thread corresponding to the condition variable 1, and does not execute the thread priority lifting operation. Wherein thread a may be the first thread and condition variable 1 is the target condition variable.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. The condition variable 1 is awakened by thread B. Thread A updates thread B and the number of wake-ups of thread B into the thread table. Because there is no thread matching with the thread B in the thread table, and there is no thread information corresponding to the condition variable 1, the thread a may directly insert the thread name and the wake-up number of the thread B into the first list corresponding to the condition variable 1. At this time, the number of wake-up times of thread B is 1.
When the thread A waits for the condition variable 1 to be awakened for the second time, the thread A can query the thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, only thread B is the thread corresponding to the condition variable 1 in the thread table. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As in fig. 8, condition variable 1 is awakened by thread B. Thread A updates thread B and the number of wake-ups of thread B into the thread table. Since there is a thread in the thread table that matches thread B, thread a increases the number of wakes for thread B by 1. At this time, the wake-up number of thread B is updated to 2. Since the thread of the condition variable 1 has only one thread B, the order of the threads from high to low based on the number of wakeups does not need to be updated.
When the thread A waits for the condition variable 1 to be awakened for the third time, the thread A can query a thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, the thread corresponding to the condition variable 1 in the thread table is thread B. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As in fig. 8, condition variable 1 is awakened by thread C. Thread a updates thread C and the number of wakes of thread C into the thread table. Since there is no thread in the thread table that matches thread C, i.e., condition variable 1 is awakened by thread C for the first time, thread a updates thread C to the last of the corresponding list of condition variable 1. At this time, the number of wake-up times of the thread C is 1. The threads of the condition variable 1 comprise threads B and threads C, the threads B are ordered according to the number of awakening times from more to less, the number of awakening times of the threads B is more than that of the threads C, and the threads B are TOP1 threads of the condition variable.
In this embodiment, in the process that the target condition variable is awakened for multiple times by the first thread, the thread information corresponding to the target condition variable in the thread table can be updated based on the thread of the actual awakening target condition variable, so that the thread information of each condition variable stored in the thread table is the information closest to the actual situation, and the accuracy of the second thread is improved.
In some embodiments, the thread information of each thread includes the occupied duration of the wake-up corresponding condition variable, and the threads corresponding to each condition variable in the thread table may be ordered from long to short according to the occupied duration, as shown in the following table 2:
TABLE 2
Table 2 includes a plurality of condition variables, for example, a condition variable 1, a condition variable 2, etc., and table 2 also includes a thread name and an occupied time length of a thread B, a thread name and an occupied time length of a thread C, etc. that can wake up the condition variable 1; including the thread name and the busy duration of thread D, thread E, etc. that can wake up condition variable 2.
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread adds the thread information of the fourth thread to the thread table according to the ordering of the occupied duration of the fourth thread in the thread table.
For example, if the condition variable 1 is the target condition variable and the fourth thread is the thread F of the wake condition variable 1, the thread table (table 2) includes the thread B and the thread C of the wake target condition variable, and there is no thread matching the thread F. In this case, the first thread Cheng Huoqu has a fourth thread occupation duration, for example, the fourth thread occupation duration is 20ms, and then according to the order of the thread occupation durations corresponding to the condition variable 1 in the thread table, the occupation duration of the thread F is less than the thread B and more than the thread C, and then the first thread may insert the thread information of the fourth thread into the next list of the thread B and be in the last list of the thread C. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
For example, if the condition variable 2 is the target condition variable and the fourth thread is the thread G of the wake condition variable 2, the thread table (table 2) includes the thread D and the thread E of the wake target condition variable, and there is no thread matching the thread G. In this case, the first thread Cheng Huoqu has a fourth thread occupation duration, for example, the fourth thread occupation duration is 30ms, and then according to the ordering of the occupation durations of the threads corresponding to the condition variable 2 in the thread table, it is determined that the occupation duration of the thread G is equal to the occupation duration of the existing thread D. In this case, the first thread may insert the thread information of the fourth thread into the next list of threads D. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
If a target third thread matched with the fourth thread exists in the thread table, the first thread updates the occupied duration of the target third thread, and the updated occupied duration of the target third thread is the sum of the occupied duration of the target third thread before updating and the occupied duration of the fourth thread.
In this embodiment, for example, the condition variable 1 is a target condition variable, and if the fourth thread is a thread B of the wake condition variable 1, the thread table (table 2) includes a thread B and a thread C of the wake target condition variable, and there is a thread matching the thread B. In this case, the occupation duration of the fourth thread of the first thread Cheng Huoqu, for example, the occupation duration of the fourth thread wake-up condition variable 1 is 10ms, the first thread updates the occupation duration of the third thread matched with the existing occupation duration of the fourth thread, the occupation duration of the fourth thread is superimposed into the occupation duration of the third thread matched with the existing occupation duration of the fourth thread, and the updated occupation duration of the third thread is the sum of the occupation duration before the update and the occupation duration of the fourth thread. For example, the occupation time before the update of the thread B in the thread table is 50ms, and the newly acquired occupation time of the current wake-up condition variable 1 of the thread B is 10ms, so that the occupation time after the update of the thread B in the thread table is 60ms.
In some embodiments, an exemplary thread table update diagram is presented in FIG. 9. Referring to fig. 9, when the thread a waits for the condition variable 1 to be awakened, thread information of the thread corresponding to the condition variable 1 in the thread may be acquired. If the condition variable 1 is never awakened, the thread information corresponding to the condition variable 1 in the thread table is empty. In this case, the thread a cannot acquire the thread corresponding to the condition variable 1, and does not execute the thread priority lifting operation. Wherein thread a may be the first thread and condition variable 1 is the target condition variable.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. The condition variable 1 is awakened by thread B. Thread A updates thread B and the length of time taken by thread B (t 1) into the thread table. Because there is no thread matching with the thread B in the thread table, and there is no thread information corresponding to the condition variable 1, the thread a may directly insert the thread name and the occupied time length (t 1) of the thread B into the first list corresponding to the condition variable 1. At this time, the occupied duration of the thread B is t1.
When the thread A waits for the condition variable 1 to be awakened for the second time, the thread A can query the thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, only thread B is the thread corresponding to the condition variable 1 in the thread table. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread B. Thread A updates thread B and the length of time taken by thread B (t 2) into the thread table. Because the thread matched with the thread B exists in the thread table, the thread A accumulates the occupied duration of the thread B. The updated occupation time of the thread B is t1+t2. Since the thread of the condition variable 1 has only one thread B, the order of the threads from long to short based on the occupied time period does not need to be updated.
When the thread A waits for the condition variable 1 to be awakened for the third time, the thread A can query a thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, the thread corresponding to the condition variable 1 in the thread table is thread B. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread C. And the thread A updates the thread C and the occupied time of the thread C into a thread table. Since there is no thread in the thread table that matches thread C, i.e., condition variable 1 is awakened by thread C for the first time. The thread A acquires the occupied time of the thread C (t 3), acquires the occupied time of the thread corresponding to the condition variable 1 in the thread table, and at the moment, the condition variable 1 only has one thread B, and the occupied time is t1+t2. Upon determining t3< t1+t2, thread A updates thread C to the next list of threads B in condition variable 1. Meanwhile, the TOP1 threads with the occupied time length of the thread B longer than that of the thread C and the thread B as a condition variable are finished by sequencing the thread list from long to short according to the occupied time length.
In this embodiment, in the process that the target condition variable is awakened for multiple times by the first thread, the thread information corresponding to the target condition variable in the thread table can be updated based on the thread of the actual awakening target condition variable, so that the thread information of each condition variable stored in the thread table is the information closest to the actual situation, and the accuracy of the second thread is improved.
In some embodiments, the thread information includes a number of wakeups and an occupied duration, and each thread in the thread table is ordered from high to low according to a weighted value of the number of wakeups and the occupied duration. Illustratively, as shown in table 3 below:
TABLE 3 Table 3
The weight coefficient of the wake-up times may be w1, and the weight coefficient of the occupied time length is w2.
Table 3 includes a plurality of condition variables, for example, a condition variable 1, a condition variable 2, etc., and table 3 also includes a thread name, a wake-up number and an occupied time length of a thread B that can wake up the condition variable 1; thread name, wake-up times, occupied time length and the like of thread C of wake-up condition variable 1; thread name, number of awakenings and duration occupied of thread D of awakening condition variable 2; thread name, number of wakeups, and duration occupied for thread E of wake condition variable 2, etc.
The method for the first thread to acquire the thread information of the fourth thread and update the thread information into the thread table can comprise the following steps:
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread adds the thread information of the fourth thread to the thread table according to the order of the weighted values of the wake-up times and the occupied time of the fourth thread in the thread table.
Illustratively, condition variable 1 is a target condition variable, e.g., w1 and w2 may both be 0.5.
If the fourth thread is thread F of wake-up condition variable 1, the thread table (table 3) includes thread B and thread C of wake-up target condition variable, and there is no thread matching with thread F. In this case, the number of wake-up times and the occupied duration of the fourth thread of the first thread Cheng Huoqu are, for example, 1, and the occupied duration is 20ms, and then according to the weighted ordering of the number of wake-up times and the occupied duration of the threads corresponding to the condition variable 1 in the thread table, the weighted value of the thread F is 10.5, which is smaller than the weighted value 26.5 of the thread B and is greater than the weighted value 6 of the thread C, and then the first thread may insert the thread information of the fourth thread into the next list of the thread B, which is the last list of the thread C. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
For example, if the condition variable 2 is the target condition variable and the fourth thread is the thread G of the wake condition variable 2, the thread table (table 3) includes the thread D and the thread E of the wake target condition variable, and there is no thread matching the thread G. In this case, the number of wake-up times and the occupied duration of the fourth thread of the first thread Cheng Huoqu are, for example, 1 for the fourth thread and 30ms for the occupied duration, and then the weighted value of the thread G is determined to be equal to the weighted value of the existing thread D according to the weighted value ranking of the number of wake-up times and the occupied duration of the threads corresponding to the condition variable 2 in the thread table, which are both 15.5. In this case, the first thread may insert the thread information of the fourth thread into the next list of threads D. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
If a target third thread matched with the fourth thread exists in the thread table, the first thread increases the awakening time of the target third thread by 1, the occupied time of the target third thread is updated, and the occupied time of the updated target third thread is the sum of the occupied time of the target third thread before updating and the occupied time of the fourth thread.
In this embodiment, for example, if the condition variable 1 is the target condition variable and the fourth thread is the thread B of the wake condition variable 1, the thread table (table 3) includes the thread B and the thread C of the wake target condition variable, and there is a thread matching the thread B. In this case, the occupation duration of the first thread Cheng Huoqu and the fourth thread, where the occupation duration of the fourth thread wake-up condition variable 1 is 10ms, the first thread updates the occupation duration of the third thread that is already matched with the occupation duration of the fourth thread, and superimposes the occupation duration of the fourth thread onto the occupation duration of the third thread that is matched with the occupation duration of the fourth thread, where the updated occupation duration of the third thread is the sum of the occupation duration before the update and the occupation duration of the fourth thread. Meanwhile, the number of wake-up times of the third thread with the fourth thread Cheng Pipei is increased by 1. For example, the occupation time before updating of the thread B in the thread table is 50ms, the wake-up times are 3, the occupation time after updating of the thread B is 60ms, and the wake-up times are 4.
In some embodiments, an exemplary thread table update diagram is presented in FIG. 10. Referring to fig. 10, when the thread a waits for the condition variable 1 to be awakened, thread information of the thread corresponding to the condition variable 1 in the thread may be acquired. If the condition variable 1 is never awakened, the thread information corresponding to the condition variable 1 in the thread table is empty. In this case, the thread a cannot acquire the thread corresponding to the condition variable 1, and does not execute the thread priority lifting operation. Wherein thread a may be the first thread and condition variable 1 is the target condition variable.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. The condition variable 1 is awakened by thread B. Thread A updates thread B and the number of wakeups (1) and the length of time occupied (t 1) of thread B into the thread table. Because there is no thread matching with the thread B in the thread table, and there is no thread information corresponding to the condition variable 1, the thread a may directly insert the thread name, the wake-up number (1), and the occupied time (t 1) of the thread B into the first list corresponding to the condition variable 1. At this time, the number of wake-up times of the thread B is 1, and the occupied time period is t1.
When the thread A waits for the condition variable 1 to be awakened for the second time, the thread A can query the thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, only thread B is the thread corresponding to the condition variable 1 in the thread table. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread B. Thread A updates thread B and the number of wakeups (1) and the length of time occupied (t 2) of thread B into the thread table. Because the thread matched with the thread B exists in the thread table, the thread A accumulates the occupied time length of the thread B, and the awakening frequency of the thread B is increased by 1. The updated occupied duration of the thread B is t1+t2, and the number of wake-up times is 1+1=2. Since the thread of the condition variable 1 has only one thread B, the order of the threads from high to low based on the weight value of the occupied time length and the number of awakenings does not need to be updated.
When the thread A waits for the condition variable 1 to be awakened for the third time, the thread A can query a thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, the thread corresponding to the condition variable 1 in the thread table is thread B. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread C. And the thread A updates the thread C and the awakening times and occupied time of the thread C into a thread table. Since there is no thread in the thread table that matches thread C, i.e., condition variable 1 is awakened by thread C for the first time. The thread A obtains the weighted value of the occupied time length (t 3) and the awakening frequency (1) of the thread C, and obtains the weighted value of the occupied time length and the awakening frequency of the thread corresponding to the condition variable 1 in the thread table. At this time, the condition variable 1 only has one thread B, the occupied time is t1+t2, the wake-up number is 2, and the weighting value is Q. Thread C has a weight of P, and when P is less than Q, thread A updates thread C to the next list of thread B in condition variable 1. Meanwhile, the ordering of the thread table from high to low according to the weight values of the occupied time length and the wake-up times is completed, the weight value of the thread B is larger than that of the thread C, and the thread B is a TOP1 thread of the conditional variable.
In this embodiment, in the process that the target condition variable is awakened for multiple times by the first thread, the thread information corresponding to the target condition variable in the thread table can be updated based on the thread of the actual awakening target condition variable, so that the thread information of each condition variable stored in the thread table is the information closest to the actual situation, and the accuracy of the second thread is improved.
Then, the first thread determines, based on the thread information of each third thread, one thread to be the second thread from the plurality of third threads, including:
The first thread determines one thread as a second thread from the plurality of third threads based on thread information of each third thread in the thread table.
In some embodiments, the third threads corresponding to each condition variable in the thread table are arranged according to a certain sequence, the first thread can obtain the third thread of TOP1 corresponding to the target condition variable from the thread table as the second thread, and perform the operation of lifting the priority of the second thread, where the thread information of each condition variable stored in the thread table is the information closest to the actual situation, so as to improve the accuracy of the second thread.
In some embodiments, the thread information includes a number of wake ups, an occupied duration, and a wake up time interval, and each thread in the thread table is ordered from high to low according to a weighted value of the number of wake ups, the occupied duration, and the wake up time interval. Illustratively, as shown in table 4 below:
TABLE 4 Table 4
The weight coefficient of the wake-up times can be w1, the weight coefficient of the occupied time length is w2, and the weight coefficient of the wake-up time interval is w3.
Table 4 includes a plurality of condition variables, for example, condition variable 1, condition variable 2, etc., and table 4 also includes a thread name, a number of wake-up times, an occupied time length, and a wake-up time interval of thread B that can wake up condition variable 1; thread name, wake-up times, occupied time length, wake-up time interval and the like of thread C of wake-up condition variable 1; thread name, wake-up times, occupied time length and wake-up time interval of thread D of wake-up condition variable 2; thread name, number of wakeups, occupied duration, wake time interval, etc. of thread E of wake condition variable 2.
The method for the first thread to acquire the thread information of the fourth thread and update the thread information into the thread table can comprise the following steps:
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread adds the thread information of the fourth thread to the thread table according to the order of the weighted values of the wake-up times, the occupied time length and the wake-up time interval of the fourth thread.
Illustratively, condition variable 1 is a target condition variable, e.g., w1 is 0.5, w2 is 0.49, and w3 is 0.01.
If the fourth thread is thread F of wake-up condition variable 1, the thread table (table 4) includes thread B and thread C of wake-up target condition variable, and there is no thread matching with thread F. In this case, the number of wake-up times, the occupied duration and the wake-up time interval of the fourth thread of the first thread Cheng Huoqu are, for example, 1, the occupied duration is 20ms, and the wake-up time interval is 300ms, and then the weighted value of the thread F is 13.3, which is smaller than the weighted value 27 of the thread B and is larger than the weighted value 8.9 of the thread C, according to the weighted value ranking of the wake-up times, the occupied duration and the wake-up time interval of the threads corresponding to the condition variable 1 in the thread table, the first thread may insert the thread information of the fourth thread into the next list of the thread B, and be in the last list of the thread C. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
Illustratively, condition variable 2 is a target condition variable, if the fourth thread is thread G of wake-up condition variable 2, the thread table (table 4) includes thread D and thread E of wake-up target condition variable, and there is no thread matching thread G. In this case, the number of wake-up times, the occupied duration and the wake-up time interval of the fourth thread of the first thread Cheng Huoqu are, for example, 1, the occupied duration is 30ms, and the wake-up time interval is 150ms, and then according to the weighted value ordering of the wake-up times, the occupied duration and the wake-up time interval of the threads corresponding to the condition variable 2in the thread table, it is determined that the weighted value of the thread G is equal to the weighted value of the existing thread D, and is 16.7. In this case, the first thread may insert the thread information of the fourth thread into the next list of threads D. The fourth thread is stored in the thread table as a newly created third thread of the wake target condition variable.
If the target third thread matched with the fourth thread exists in the thread table, the first thread increases the awakening frequency of the target third thread by 1, and the occupied time length and the awakening time interval of the target third thread are updated.
The occupied time of the updated target third thread is the sum of the occupied time of the target third thread before updating and the occupied time of the fourth thread. The wake-up time interval of the updated target third thread is the sum of the wake-up time interval of the target third thread and the wake-up time interval of the fourth thread before updating.
In this embodiment, for example, if the condition variable 1 is the target condition variable and the fourth thread is the thread B of the wake condition variable 1, the thread table (table 4) includes the thread B and the thread C of the wake target condition variable, and there is a thread matching the thread B. In this case, the occupation duration of the first thread Cheng Huoqu and the fourth thread, where the occupation duration of the fourth thread wake-up condition variable 1 is 10ms, the first thread updates the occupation duration of the third thread that is already matched with the occupation duration of the fourth thread, and superimposes the occupation duration of the fourth thread onto the occupation duration of the third thread that is matched with the occupation duration of the fourth thread, where the updated occupation duration of the third thread is the sum of the occupation duration before the update and the occupation duration of the fourth thread. The wake-up time interval of the fourth thread of the first thread Cheng Huoqu, the wake-up time interval of the wake-up condition variable 1 of the fourth thread is 100ms, the first thread updates the existing wake-up time interval of the third thread matched with the first thread, the wake-up time interval of the fourth thread is overlapped in the wake-up time interval of the third thread matched with the first thread, and the wake-up time interval of the third thread after updating is the sum of the wake-up time interval before updating and the wake-up time interval of the fourth thread. Meanwhile, the number of wake-up times of the third thread with the fourth thread Cheng Pipei is increased by 1. For example, the occupation time of the thread B before updating in the thread table is 50ms, the wake-up time is 3, the wake-up time interval is 100ms, the occupation time of the updated thread B is 60ms, the wake-up time is 4, and the wake-up time interval is 200ms.
In some embodiments, an exemplary thread table update diagram is presented in FIG. 11. Referring to fig. 11, when the thread a waits for the condition variable 1 to be awakened, thread information of the thread corresponding to the condition variable 1 in the thread may be acquired. If the condition variable 1 is never awakened, the thread information corresponding to the condition variable 1 in the thread table is empty. In this case, the thread a cannot acquire the thread corresponding to the condition variable 1, and does not execute the thread priority lifting operation. Wherein thread a may be the first thread and condition variable 1 is the target condition variable.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. The condition variable 1 is awakened by thread B. Thread A updates thread B and the number of wake-ups (1), the occupied time (t 1) and the wake-up time interval (t 11) of thread B into a thread table. Because the thread table does not have a thread matched with the thread B and does not have the thread information corresponding to the condition variable 1, the thread A can directly insert the thread name, the wake-up times (1), the occupied time (t 1) and the wake-up time interval (t 11) of the thread B into the first list corresponding to the condition variable 1. At this time, the number of wake-up times of the thread B is 1, the occupied time period is t1, and the wake-up time interval is t11.
When the thread A waits for the condition variable 1 to be awakened for the second time, the thread A can query the thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, only thread B is the thread corresponding to the condition variable 1 in the thread table. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread B. Thread A updates thread B and thread B's wake-up times (1), occupancy time (t 2) and wake-up time interval (t 22) into the thread table. Because the thread matched with the thread B exists in the thread table, the thread A accumulates the occupied time length of the thread B, accumulates the wake-up time interval and increases the wake-up times of the thread B by 1. The updated occupied duration of the thread B is t1+t2, the wake-up time interval is t11+t22, and the wake-up times are 1+1=2. Since the thread of the condition variable 1 has only one thread B, the order of the threads from high to low based on the weight value of the occupied time length and the number of awakenings does not need to be updated.
When the thread A waits for the condition variable 1 to be awakened for the third time, the thread A can query a thread table to acquire the TOP1 thread corresponding to the condition variable 1. At this time, the thread corresponding to the condition variable 1 in the thread table is thread B. Thread B is the second thread to wake condition variable 1. After thread a obtains thread information (e.g., a thread name) of thread B, thread a raises the priority of thread B.
After the condition variable 1 is awakened, thread a may obtain thread information for the thread awakening the condition variable 1. As shown, condition variable 1 is awakened by thread C. Thread A updates thread C and thread C wake-up times (1), occupied time (t 3) and wake-up time interval (t 33) into the thread table. Since there is no thread in the thread table that matches thread C, i.e., condition variable 1 is awakened by thread C for the first time. The thread A acquires the weight values of the occupied time length (t 3), the awakening times (1) and the awakening time interval (t 33) of the thread C, and acquires the weight values of the occupied time length, the awakening times and the awakening time interval of the thread corresponding to the condition variable 1 in the thread table. At this time, the condition variable 1 only has one thread B, which occupies a time period of t1+t2, a wake-up number of times of 2, a wake-up time interval of t11+t22, and a weight value of M. Thread C has a weight of N, and when N is less than M, thread A updates thread C to the next list of threads B in condition variable 1. Meanwhile, the sequencing of the thread table from high to low according to the occupied time length, the wake-up times and the weighted value of the wake-up time interval is completed, the weighted value of the thread B is larger than the weighted value of the thread C, and the thread B is a TOP1 thread of the conditional variable.
In this embodiment, in the process that the target condition variable is awakened for multiple times by the first thread, the thread information corresponding to the target condition variable in the thread table can be updated based on the thread of the actual awakening target condition variable, so that the thread information of each condition variable stored in the thread table is the information closest to the actual situation, and the accuracy of the second thread is improved.
Then, the first thread determines, based on the thread information of each third thread, one thread to be the second thread from the plurality of third threads, including:
The first thread determines one thread as a second thread from the plurality of third threads based on thread information of each third thread in the thread table.
In some embodiments, the third threads corresponding to each condition variable in the thread table are arranged according to a certain sequence, the first thread can obtain the third thread of TOP1 corresponding to the target condition variable from the thread table as the second thread, and perform the operation of lifting the priority of the second thread, where the thread information of each condition variable stored in the thread table is the information closest to the actual situation, so as to improve the accuracy of the second thread.
Embodiments of the present application also provide a computer-readable storage medium including computer instructions that, when executed on an electronic device described above, cause the electronic device to perform the functions or steps performed by the electronic device 100 in the method embodiments described above.
Embodiments of the present application also provide a computer program product which, when run on a computer, causes the computer to perform the functions or steps performed by the electronic device 100 in the method embodiments described above. For example, the computer may be the electronic device 100 described above.
It will be apparent to those skilled in the art from this description that, for convenience and brevity of description, only the above-described division of the functional modules is illustrated, and in practical application, the above-described functional allocation may be performed by different functional modules according to needs, i.e. the internal structure of the apparatus is divided into different functional modules to perform all or part of the functions described above.
In the several embodiments provided by the present application, it should be understood that the disclosed apparatus and method may be implemented in other manners. For example, the apparatus embodiments described above are merely illustrative, e.g., the division of the modules or units is merely a logical functional division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another apparatus, or some features may be omitted, or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be an indirect coupling or communication connection via some interfaces, devices or units, which may be in electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and the parts displayed as units may be one physical unit or a plurality of physical units, may be located in one place, or may be distributed in a plurality of different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in the embodiments of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit. The integrated units may be implemented in hardware or in software functional units.
The integrated units, if implemented in the form of software functional units and sold or used as stand-alone products, may be stored in a readable storage medium. Based on such understanding, the technical solution of the embodiments of the present application may be essentially or a part contributing to the prior art or all or part of the technical solution may be embodied in the form of a software product stored in a storage medium, including several instructions for causing a device (may be a single-chip microcomputer, a chip or the like) or a processor (processor) to perform all or part of the steps of the method described in the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read Only Memory (ROM), a random access memory (random access memory, RAM), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The foregoing is merely illustrative of specific embodiments of the present application, but the scope of the present application is not limited thereto, and any changes or substitutions within the technical scope of the present application should be covered by the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims (10)

1. A method for processing a thread, the method being applied to an electronic device, the electronic device including a condition variable module and a plurality of threads, the plurality of threads including a first thread and a second thread, the method comprising:
Under the condition that the first thread pauses to execute a first task due to the fact that a target condition variable is awakened, the first thread acquires thread information of a second thread corresponding to the target condition variable from the condition variable module, the second thread awakens the target condition variable by executing a second task, and before the second thread executes the second task, the second thread writes the thread information of the second thread into the condition variable module;
the priority of the first thread Cheng Disheng and the second thread;
And when the second thread wakes up the target condition variable by executing the second task, waking up the first thread to continue executing the first task by sending a wakeup broadcast.
2. The method of claim 1, wherein the plurality of threads includes a third thread, the third thread being a thread that wakes up the target condition variable by performing a third task; the thread information of the second thread of the first thread Cheng Huoqu includes:
the first thread determines one thread as the second thread from a plurality of third threads based on the thread information of each third thread, and acquires the thread information of the second thread.
3. The method according to claim 2, wherein the thread information includes a number of wakeups of the target condition variable by each of the third threads Cheng Huanxing and/or an occupied period of time for each of the third threads to wake up the target condition variable by executing the third task;
the first thread determines, based on thread information of each third thread, one thread as the second thread from a plurality of third threads, including:
When the thread information comprises the awakening times, the first thread determines one third thread with the highest awakening times as the second thread based on the awakening times of the third threads;
Or when the thread information includes the occupied duration, the first thread determines, based on the occupied duration of each third thread, that one third thread with the longest occupied duration is the second thread;
Or when the thread information comprises the awakening times and the occupied time, the first thread determines one third thread as the second thread based on the awakening times and the occupied time of each third thread; the more the number of wake-up times of the third thread is, the greater the probability that the third thread is the second thread is; the longer the occupied duration of the third thread, the greater the probability that the third thread is the second thread.
4. The method of claim 2, wherein the condition variable module includes a thread table, and wherein the thread table includes at least one of the third threads;
The method further comprises the steps of:
after the target condition variable is awakened, the first thread acquires thread information of a fourth thread awakening the target condition variable;
the first thread adds the thread information of the fourth thread to the thread table;
the first thread determines, based on thread information of each third thread, one thread as the second thread from a plurality of third threads, including:
The first thread determines one thread as the second thread from a plurality of third threads based on the thread information of each third thread in the thread table.
5. The method of claim 4, wherein the thread information includes a number of wakeups for each of the target condition variables for each of the third threads Cheng Huanxing, each of the threads in the thread table being ordered from more to less of the number of wakeups;
The first thread adding the thread information of the fourth thread to the thread table, including:
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread creates a new third thread in the thread table based on the thread information of the fourth thread according to the ordering of the wake-up times of the fourth thread in the thread table;
If a target third thread matched with the fourth thread exists in the thread table, the first thread increases the awakening frequency of the target third thread by 1.
6. The method of claim 4, wherein the thread information includes an occupied time period for each of the third threads to execute the third task to wake up the target condition variable, and wherein each of the threads in the thread table is ordered from long to short according to occupied time period;
The first thread adding the thread information of the fourth thread to the thread table, including:
If the third thread of the fourth thread Cheng Pipei does not exist in the thread table, the first thread creates a new third thread in the thread table based on the thread information of the fourth thread according to the ordering of the occupied duration of the fourth thread in the thread table;
If the target third thread matched with the fourth thread exists in the thread table, the first thread updates the occupied duration of the target third thread, and the updated occupied duration of the target third thread is the sum of the occupied duration of the target third thread before updating and the occupied duration of the fourth thread.
7. The method of claim 1, wherein the plurality of threads comprises a fifth thread comprising a thread that is executing a task and/or a thread that is waiting to execute a task in addition to the second thread, the fifth thread having a priority that is higher than or equal to an initial priority of the second thread; the priority of the second thread by the first thread Cheng Disheng includes:
the first thread determines the target priority of the second thread based on the priorities of the fifth threads;
The first thread increases the initial priority of the second thread to the target priority.
8. The method of claim 7, wherein after the first thread continues to execute the first task, the method further comprises:
the first thread adjusts the priority of the second thread to the initial priority.
9. An electronic device comprising a memory and one or more processors; the memory is coupled with the processor; the memory has stored therein computer program code comprising computer instructions which, when executed by the processor, cause the electronic device to perform the method of any of claims 1-8.
10. A computer readable storage medium comprising computer instructions which, when run on an electronic device, cause the electronic device to perform the method of any of claims 1-8.
CN202311561678.2A 2023-11-22 2023-11-22 Thread processing method and electronic equipment Active CN117271144B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311561678.2A CN117271144B (en) 2023-11-22 2023-11-22 Thread processing method and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311561678.2A CN117271144B (en) 2023-11-22 2023-11-22 Thread processing method and electronic equipment

Publications (2)

Publication Number Publication Date
CN117271144A CN117271144A (en) 2023-12-22
CN117271144B true CN117271144B (en) 2024-04-23

Family

ID=89206724

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311561678.2A Active CN117271144B (en) 2023-11-22 2023-11-22 Thread processing method and electronic equipment

Country Status (1)

Country Link
CN (1) CN117271144B (en)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111767124A (en) * 2020-06-03 2020-10-13 Oppo广东移动通信有限公司 Request response method, device, storage medium and electronic equipment
CN116501476A (en) * 2023-06-28 2023-07-28 成都赛力斯科技有限公司 Processing method, device, equipment and medium for linux universal character equipment
CN116932194A (en) * 2022-04-07 2023-10-24 深圳Tcl新技术有限公司 Thread execution method, thread execution device, electronic equipment and computer readable storage medium

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20070136725A1 (en) * 2005-12-12 2007-06-14 International Business Machines Corporation System and method for optimized preemption and reservation of software locks
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111767124A (en) * 2020-06-03 2020-10-13 Oppo广东移动通信有限公司 Request response method, device, storage medium and electronic equipment
CN116932194A (en) * 2022-04-07 2023-10-24 深圳Tcl新技术有限公司 Thread execution method, thread execution device, electronic equipment and computer readable storage medium
CN116501476A (en) * 2023-06-28 2023-07-28 成都赛力斯科技有限公司 Processing method, device, equipment and medium for linux universal character equipment

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
基于优先级的Java线程唤醒机制研究与实现;王溪波 等;《沈阳建筑大学学报(自然科学版)》;20080331;第24卷(第2期);第328-332页 *

Also Published As

Publication number Publication date
CN117271144A (en) 2023-12-22

Similar Documents

Publication Publication Date Title
EP2893444B1 (en) Quota-based resource management
US10133598B2 (en) Systems and methods of using a hypervisor to assign virtual processor priority based on task priority and to schedule virtual processors for guest operating systems
EP2473914B1 (en) Hardware-based scheduling of graphics processor unit (gpu) work
CN110489213B (en) Task processing method and processing device and computer system
WO2021057643A1 (en) Multi-thread synchronization method and electronic device
CN112416546A (en) Multitask scheduling method, electronic device and computer storage medium
EP2284703B1 (en) Scheduling of tasks in a parallel computer system according to defined policies
CN108549574B (en) Thread scheduling management method and device, computer equipment and storage medium
US20100115525A1 (en) Method for dynamically enabling the expansion of a computer operating system
CN113495780A (en) Task scheduling method and device, storage medium and electronic equipment
CN114637536A (en) Task processing method, computing coprocessor, chip and computer equipment
CN114625533A (en) Distributed task scheduling method and device, electronic equipment and storage medium
CN115509704A (en) Task scheduling method, device, equipment and storage medium
CN112925616A (en) Task allocation method and device, storage medium and electronic equipment
CN112231077B (en) Application scheduling method and electronic equipment
CN117271144B (en) Thread processing method and electronic equipment
CN111813541B (en) Task scheduling method, device, medium and equipment
CN113495787A (en) Resource allocation method, device, storage medium and electronic equipment
CN111831436A (en) Scheduling method and device of IO (input/output) request, storage medium and electronic equipment
EP4220403A1 (en) Service processing method and related apparatus
CN116302558A (en) Thread scheduling method and device and electronic equipment
CN113032154B (en) Scheduling method and device for virtual CPU, electronic equipment and storage medium
JP2022079764A (en) Synchronous control system and synchronous control method
US7111298B1 (en) Inter-processor competition for a shared resource
CN116303110B (en) Memory garbage recycling method and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant