A method, system, device, and storage medium for tracking thread scheduling switches for a task

By encapsulating the basic class for thread creation, setting thread names, and recording functions, the problem of not being able to obtain switching information in thread scheduling is solved, thus achieving efficient scheduling and acceleration of task execution.

CN114253678BActive Publication Date: 2026-01-06武汉斗鱼鱼乐网络科技有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202011009121.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-23
Publication Date
2026-01-06
Estimated Expiration
2040-09-23

AI Technical Summary

Technical Problem

In existing technologies, the switching information between threads cannot be obtained during thread scheduling, resulting in low task execution efficiency.

Method used

By encapsulating a thread creation interface, using a map memory and thread locks to set thread names, detecting whether developers use the interface to create threads, setting up a base class with thread data recording capabilities, and automatically completing thread switching statistics through the execution of asynchronous tasks.

Benefits of technology

Accurately record thread status during task execution, optimize task scheduling, and improve task execution efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114253678B_ABST
    Figure CN114253678B_ABST
Patent Text Reader

Abstract

The application provides a thread scheduling switching tracking method, system, device and storage medium for a task, the method comprising: encapsulating a thread creation interface, creating a thread by using the thread creation interface; setting a thread name for the thread created by using the thread creation interface by using a map memory and a thread lock setting mode; detecting whether a developer establishes a thread by using the thread creation interface; setting a basic class with a thread data recording function, determining whether the thread of the current task needs to perform a switching operation; setting asynchronous task execution and thread delivery, and automatically completing thread switching statistics by using asynchronous task execution. The system comprises modules corresponding to the steps of the method; and the device comprises a processor and a memory.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention proposes a method, system, device, and storage medium for tracking thread scheduling and switching of tasks, belonging to the field of data processing technology. Background Technology

[0002] Existing programs typically utilize asynchronous tasks and employ multithreading or thread pools to schedule task execution. However, as thread pools and asynchronous task scheduling become more complex, it becomes difficult to discern which threads a task traverses during execution, leading to complex thread switching between tasks. Switching between different threads consumes resources, requiring thread context switching. Therefore, a method is needed to determine which threads a task traverses from start to finish, and which threads it switches to. With this thread switching information, we can optimize task scheduling, aiming to complete tasks within a single thread from beginning to end, thereby accelerating task execution. Summary of the Invention

[0003] This invention provides a method, system, device, and storage medium for tracking thread scheduling and switching of tasks, to solve the problem that switching information between threads cannot be obtained in existing thread scheduling processes. The technical solution adopted is as follows: A method, system, device, and storage medium for tracking thread scheduling and switching of tasks, wherein the method for tracking thread scheduling and switching of tasks includes:

[0004] Encapsulate a thread creation interface, and use the thread creation interface to create threads;

[0005] The thread name is set for the thread created by the creation interface by using a map memory and setting a thread lock;

[0006] Detect whether the developer has used the thread creation interface to create a thread;

[0007] Set up a base class with thread data logging capabilities to determine whether the thread of the current task needs to be switched.

[0008] Configure the execution and thread delivery of asynchronous tasks, and use the execution of asynchronous tasks to automatically complete the thread switching statistics;

[0009] Specifically, the method of using a map memory and setting a thread lock to set the thread name for the thread created by the creation interface includes:

[0010] A map storage is used to map the correspondence between threads and thread names, forming a stored mapping table;

[0011] Set a thread lock;

[0012] Get the ID corresponding to the thread;

[0013] Use a lambda function to pass the thread name as a parameter.

[0014] The mapThread variable is written to the thread using the write lock in the thread lock.

[0015] The thread name corresponding to the thread ID is stored, and then the thread function of the thread is executed, and the thread begins to execute the corresponding task;

[0016] Furthermore, all the logic for setting the thread name is written in the macro #ifdef Thread_Count.

[0017] Furthermore, the detection of whether the developer uses the thread creation interface to establish a thread includes:

[0018] Write a hook function to hook the system's thread creation and obtain the call stack of the thread creation.

[0019] Determine if the call stack contains a call to the createThread function. If not, it means the thread created by the developer was not created using the thread creation interface; if it does, it means the thread created by the developer was created using the thread creation interface.

[0020] If it is determined that the developer did not use the thread creation interface to create the thread, an error message will be output, prompting the developer to use the thread creation interface to create the thread.

[0021] Furthermore, the process of setting up the base class with thread data recording functionality includes:

[0022] Set a member variable and use the member variable to record the ID of the current thread;

[0023] Set up a linked list and use the linked list to store the thread IDs that the task has gone through in the order of the threads the task has gone through;

[0024] Set up a constructor, initialize the ID corresponding to the thread in the constructor, provide a scheduling interface for task switching, and thus record the thread of the task;

[0025] Set a destructor to indicate that the task will be consumed soon and that the task has been completed.

[0026] Output all threads and their names during the execution of the task;

[0027] The process of initializing the thread ID in the constructor, providing a task switching scheduling interface, and recording the task's thread includes:

[0028] Get the ID of the current thread;

[0029] Check if the current thread ID is equal to the previously executed thread ID. If the current thread ID is not equal to the previously executed thread ID, store the current thread ID and assign it the new ID. If the current thread ID is equal to the previously executed thread ID, it means that they are the same thread and there is no need to store the thread ID.

[0030] The new ID is used as the current thread's current_id, and the current_id is stored in the linked list to indicate that the current task has a thread switching operation.

[0031] Furthermore, the process of executing the task and outputting all threads and their names includes:

[0032] Use read locks to allow multiple threads to read simultaneously;

[0033] The corresponding thread name is read based on the mapping table stored in the map memory;

[0034] Write the thread name to the log file or print it out.

[0035] Furthermore, the setting of asynchronous task execution and thread delivery, and the automatic completion of thread switching statistics using the execution of asynchronous tasks, includes:

[0036] Based on the number of functions required to complete the task, an asynchronous task execution function is added to each function; the asynchronous task execution function has the function of detecting the execution of the current thread and can automatically complete the statistical function of thread switching.

[0037] Calling the Dispatch() interface of the asynchronous task's execution function will automatically complete the thread switching statistics.

[0038] Furthermore, the setting of asynchronous task execution and thread delivery, and the automatic completion of thread switching statistics using the execution of asynchronous tasks, includes:

[0039] Write a script to scan all classes in the code during the execution of asynchronous tasks and thread dispatching, and check if they inherit from the ThreadRecod class. If they do, find the public methods of this class and automatically add a Dispatch() call to each method.

[0040] Scan all subclasses of the classes in the code to see if they inherit from the ThreadRecod class. If they do, find the public methods of that class and automatically add a Dispatch() call to each method.

[0041] The Dispatch() interface of the execution function will automatically complete the thread switching statistics.

[0042] A system for tracking thread scheduling and switching of tasks, the system comprising:

[0043] The thread creation module encapsulates a thread creation interface, which is used to create threads.

[0044] The thread name setting module is used to set the thread name for the thread created by the creation interface by using a map memory and setting a thread lock.

[0045] The thread detection module is used to detect whether the developer has used the thread creation interface to create a thread.

[0046] The switch determination module is used to set up a base class with thread data recording capabilities to determine whether the thread of the current task needs to be switched.

[0047] The statistics module is used to set the execution of asynchronous tasks and thread delivery, and to automatically complete the thread switching statistics by using the execution of asynchronous tasks;

[0048] The thread name setting module includes:

[0049] The mapping table forming module is used to map the correspondence between threads and thread names using the map storage to form a stored mapping table;

[0050] The thread lock setting module is used to set a thread lock;

[0051] The ID acquisition module is used to obtain the ID corresponding to the thread.

[0052] The input module is used to pass the thread name as a parameter using a lambda function;

[0053] The write module is used to write the mapThread variable to the thread using the write lock in the thread lock;

[0054] An execution module is used to store the thread name corresponding to the thread ID, and then execute the thread function of the thread, so that the thread begins to execute the corresponding task;

[0055] All the logic for setting the thread name is written in the macro #ifdef Thread_Count.

[0056] A device for tracking thread scheduling switching of tasks, the device comprising:

[0057] Processing unit;

[0058] Memory configured to store instructions executable by a storage processing unit;

[0059] The processing unit is configured to implement any one of the methods described above during execution.

[0060] A non-transitory computer-readable storage medium includes: a storage medium storing computer-executable instructions, which, when executed by a processing unit, implement any one of the methods described above.

[0061] Beneficial effects of this invention:

[0062] This invention proposes a method, system, device, and storage medium for tracking thread scheduling and switching in tasks. By naming threads with corresponding thread names, it can accurately and clearly record the thread status during task execution, facilitating observation and understanding of the specific task execution process. Furthermore, this invention provides a method, system, device, and storage medium for tracking thread scheduling and switching in tasks, accurately obtaining the execution and order of all threads a task traverses from start to finish, and accurately obtaining the specific details of thread switching. With this thread switching information, task scheduling can be optimized, allowing tasks to be executed from beginning to end within a single thread as much as possible, thereby accelerating task execution. Attached Figure Description

[0063] Figure 1 This is a flowchart of the method described in this invention;

[0064] Figure 2 This is a schematic diagram of the system described in this invention. Figure 1 ;

[0065] Figure 3 This is a schematic diagram of the system described in this invention. Figure 2 ;

[0066] Figure 4 This is a schematic diagram of the device described in this invention;

[0067] Figure 5 This is a schematic diagram of the storage medium described in this invention. Detailed Implementation

[0068] The preferred embodiments of the present invention will be described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are for illustration and explanation only and are not intended to limit the present invention.

[0069] This invention provides a method, system, device, and storage medium for tracking thread scheduling and switching of tasks, to solve the problem that existing thread scheduling processes cannot obtain switching information between threads. The technical solution adopted is as follows:

[0070] This invention provides a method, system, device, and storage medium for tracking thread scheduling and switching of tasks, such as... Figure 1 As shown, the method for thread scheduling switching of the tracing task includes:

[0071] Step 1: Encapsulate a thread creation interface `createThread`, and use this interface to create threads.

[0072] Step 2: Use a map memory and a thread lock to set the thread name for the thread created by the creation interface;

[0073] Step 3: Check whether the developer has used the thread creation interface to create a thread;

[0074] Step 4: Set up a base class with thread data recording functionality to determine whether the thread of the current task needs to be switched.

[0075] Step 5: Configure the execution of asynchronous tasks and thread delivery, and use the execution of asynchronous tasks to automatically complete the thread switching statistics.

[0076] The working principle of the above technical solution is as follows: First, a thread creation interface `createThread` is encapsulated, and a thread is created using the thread creation interface; then, a thread name is set for the thread created by the creation interface using a map memory and a thread lock; subsequently, it is detected whether the developer has used the thread creation interface to create the thread; then, a base class with thread data recording function is set to determine whether the thread of the current task needs to be switched; finally, the execution of asynchronous tasks and thread delivery are set, and the execution of asynchronous tasks is used to automatically complete the thread switching statistics.

[0077] The above technical solution achieves the following effects: by naming threads with corresponding thread names, the execution status of threads during task execution can be accurately and clearly recorded, facilitating observation and understanding of the specific task execution process. Furthermore, the method, system, device, and storage medium for tracking task thread scheduling and switching proposed in this invention can accurately obtain the execution and execution order of all threads a task undergoes from start to finish, and accurately obtain the specific details of thread switching. With this thread switching information, task scheduling can be optimized, allowing tasks to be executed from beginning to end within a single thread as much as possible, thereby accelerating task execution.

[0078] In one embodiment of the present invention, step 2, which involves setting the thread name for the thread created by the creation interface using a map memory and a thread lock, includes:

[0079] Step 201: Use a map storage to map the correspondence between threads and thread names, forming a stored mapping table;

[0080] Step 202: Set a thread lock for multi-threaded reading and writing of the mapThread variable;

[0081] Step 203: Obtain the ID corresponding to the thread. This is the ID of the newly created thread.

[0082] Step 204: Pass the thread name as a parameter using a lambda function.

[0083] Step 205: Use the write lock in the thread lock to write the mapThread variable to the thread;

[0084] Step 206: Store the thread name corresponding to the thread ID, and then execute the thread function of the thread, so that the thread begins to execute the corresponding task.

[0085] The working principle of the above technical solution is as follows:

[0086] First, an interface for thread creation is encapsulated. All thread creation must use this interface. Simultaneously, the thread name is set using steps 201 to 206 above. This effectively prevents developers from creating threads without using this interface, which would result in threads without names. The specific implementation principle of steps 201 to 206 above is as follows:

[0087] thread createThread(string name,func){

[0088] First, a thread is created. Then, the thread's ID is obtained, and a map is used to map the correspondence between the thread and its name.

[0089] Map<Threadid,string> mapThread; where the key is Threadid and the value is the name of the thread.

[0090] Mutex lock sets a thread lock for multi-threaded reading and writing of mapThread variables.

[0091] Since this involves creating a thread, the thread's ID is retrieved and executed within the newly created thread's code. Furthermore, a function is created and executed before the thread's specific task, `func`, is executed. The advantage of this approach is that the thread's `mapThread` data is stored immediately before the thread's task is executed, thus avoiding the potential issue of a slight time lag if the thread is created first, causing the thread to start executing before the `mapThread` variable has been set.

[0092] std::thread thread([name](){Uses the write lock in the thread lock to write the mapThread variable to the thread.

[0093] For convenience, a lambda expression is used, with the thread name passed as a parameter. This lambda function executes within the created thread, allowing for timely retrieval of the thread ID.

[0094] Threadid id = this_thread::get_id(); First, get the thread ID. This is the ID of the newly created thread.

[0095] The `Mutex::Write holder(lock.get())` method uses the write lock in the thread lock to write the `mapThread` variable to the thread. Since the data is written through the write lock, the problem of asynchronous data reading by other threads can be avoided.

[0096] mapThread[id] = name; stores the thread name corresponding to id;

[0097] Then, the thread's thread function is executed, and the thread begins to execute its corresponding task.

[0098] });

[0099] The advantages of the above technical solution are as follows: Because this invention proposes a method for optimizing task scheduling across threads, it ultimately outputs the thread history of each task, showing which thread it started executing from and which it started executing from. However, since each thread is uniquely identified by a single threadid (a numeric value), the output information uses numbers. This makes searching for numbers in the output inconvenient and makes it impossible to understand which specific thread each number represents. Furthermore, threads themselves do not have names. Therefore, this embodiment considers requiring thread creators to set a name for the thread to indicate its purpose, ensuring the name conveys the thread's meaning. Since the system does not support naming threads, this embodiment proposes the thread naming method described above to address the issue of the system's lack of support for thread names.

[0100] The above method of setting thread names allows for naming of each thread even when the system does not support thread names, thus establishing a mapping between threads and their corresponding thread names. Furthermore, this method effectively avoids the problem of missed mapThread variable settings caused by time intervals resulting from thread creation. This significantly improves the stability and accuracy of thread name settings, thereby enhancing the accuracy and operational stability of subsequent thread recording, scheduling, and switching.

[0101] In one embodiment of the present invention, all the logic for setting the thread name is written in the macro #ifdef Thread_Count.

[0102] The effect of the above technical solution is that all the logic for mapping the names of threads must be written in the macro #ifdef Thread_Count. This way, when the program is released, such logic will not be executed, thus avoiding wasting performance on counting thread names.

[0103] In one embodiment of the present invention, step 3, detecting whether the developer has used the thread creation interface to establish a thread, includes:

[0104] Step 301: Write a hook function to hook the system's thread creation and obtain the call stack of thread creation.

[0105] Step 302: Determine if there is a call stack for the createThread function in the call stack. If not, it means that the thread created by the developer was not created using the thread creation interface; if so, it means that the thread created by the developer was created using the thread creation interface.

[0106] Step 303: When it is determined that the developer did not use the thread creation interface to create the thread, an error message is output, prompting the developer to use the thread creation interface to create the thread.

[0107] All the above logic for hooking threads must be written in the macro #ifdef Thread_Count, so that this logic will not be executed when the program is released, thus avoiding wasting performance on hooking thread functions.

[0108] The working principle of the above technical solution is as follows: by writing a hook function, the system's thread creation is hooked, and the call stack of thread creation is obtained. Only threads created through the thread creation interface createThread function will have their thread creation call stack created from createThread. Otherwise, if the system thread is called directly, there will be no call stack of createThread function. In this way, thread creation can be monitored in real time. Once a developer creates a thread without calling the thread creation interface createThread, the system will immediately output an error message to prompt the developer to use createThread to create the thread.

[0109] The above technical solution achieves the following results: It effectively prevents developers from creating threads without using the `createThread` interface, thus significantly improving the accuracy of subsequent thread recording and preventing missed thread entries. Simultaneously, it effectively prevents the problem of not being able to find the thread name later when developers use system-provided interfaces instead of the pre-constructed `createThread` interface. This improves the accuracy and efficiency of thread name tracking.

[0110] In one embodiment of the present invention, step 4, the process of setting up the base class with thread data recording functionality, includes:

[0111] Step 401: Set member variables and use the member variables to record the ID of the current thread;

[0112] Step 402: Set up a linked list and use the linked list to store the thread IDs that the task has gone through in the order of the threads the task has gone through;

[0113] Step 403: Set the constructor, initialize the id corresponding to the thread in the constructor, provide the scheduling interface for task switching, and thus record the thread of the task;

[0114] Step 404: Set up a destructor function to indicate that the task will be consumed soon, indicating that the task has been completed.

[0115] Step 405: Output all threads and their names during the execution of the task, so that it is easy to know which threads the task execution went through.

[0116] Specifically, step 402 involves initializing the thread's corresponding ID in the constructor, providing a task switching scheduling interface, and thus recording the task's thread. This process includes:

[0117] Step 4031: Obtain the ID of the current thread;

[0118] Step 4032: Determine if the current thread ID is equal to the previously executed ID. If the current thread ID is not equal to the previously executed ID, store the current thread ID and assign it a new ID. If the current thread ID is equal to the previously executed ID, it means that they are the same thread and there is no need to store the thread ID.

[0119] Step 4033: Use the new ID as the latest thread current_id, and store the thread current_id in the linked list to indicate that the current task has a thread switching operation.

[0120] Step 405, which involves outputting all threads and their names during the execution of the task, includes the following steps:

[0121] Step 4051: Use a read lock to simultaneously read data from multiple threads.

[0122] Step 4052: Read the corresponding thread name based on the mapping table stored in the map memory;

[0123] Step 4053: Write the thread name to a log file or print it out.

[0124] The working principle of the above technical solution is as follows: the function of recording threads is implemented in the base class, so that all functions inheriting from this class have the function of recording threads. The specific design is as follows:

[0125] Class ThreadRecod{

[0126] First, design a member variable to record the current thread ID:

[0127] Threadid current_id; This variable is used to record the current thread ID.

[0128] List <threadid>list_id; Here, a linked list is used to store the thread IDs that the task has gone through, maintaining the order of the process. The IDs of the first thread are stored at the beginning of the linked list, and the IDs of the last thread are stored at the end of the linked list.

[0129] Next, we will write the interface for this class:

[0130] A constructor and destructor must be implemented. The constructor retrieves the first thread ID, and the destructor retrieves the last thread ID. This is because the object's construction is the first point of execution for that object, so the thread ID obtained at this point is the earliest executing thread. The destructor retrieves the thread ID from the last execution of the object, thus identifying the last executing thread. Therefore, the object's construction is used here to initialize and terminate the thread ID.

[0131] First, the constructor

[0132] ThreadRecod(){

[0133] In the constructor, initialize current_id = this_thread::get_id();

[0134] }

[0135] Provides a scheduling interface for task switching, thereby recording the thread of the task.

[0136] Void Dispatch(){

[0137] Threadid id = this_thread::get_id(); First, get the ID of the current thread;

[0138] If (id != current_id) { If the current id is not equal to the previously executed id, then store it and assign it as the new id.

[0139] current_id = id; assign this id to the current latest thread's current_id.

[0140] The function `list_id.push_back(id)` is called to push back the thread ID to a linked list, indicating that a thread switch operation has occurred in the current task.

[0141] Else {Otherwise, it indicates that it is the same thread, so there is no need to store any thread ID}

[0142] Next, we will write the destructor.

[0143] ~ThreadRecod(){

[0144] The destructor indicates that the task will be consumed soon, indicating that the task has been completed.

[0145] Threadid id = this_thread::get_id(); First, get the ID of the current thread;

[0146] If (id != current_id) { If the current id is not equal to the previously executed id, then store it and assign it as the new id.

[0147] current_id = id; assign this id to the current latest thread's current_id.

[0148] The function `list_id.push_back(id)` is called to push back the thread ID to a linked list, indicating that a thread switch operation has occurred in the current task.

[0149] }

[0150] Next, we need to output all the threads and their names during the execution of this task, so that we can easily know which threads the task execution went through.

[0151] Mutex::Read holder(lock.get()); A read lock must be used here. This is only for reading data and not for modifying it. Using a read lock allows multiple threads to read at the same time. In reality, multiple objects from multiple threads may be destroyed at the same time, and each time an object is destroyed, this query thread name will be called.

[0152] For(auto it:list_id){

[0153] String name = mapThread[id]; retrieves the corresponding thread name 'name' based on the stored mapping table.

[0154] Then the current name can be written to a log file or printed out.

[0155] Cout<<"threadid:”< <id<<"threadName:”<<name;

[0156] }

[0157] The iteration of the above function will print out all threads involved in the execution of this task.

[0158] Additionally, since the thread statistics functionality described earlier in this article is omitted for ease of program release, the following code can be included using a compilation macro. This way, if the macro is defined in the code segment during program release, the following code will not be executed; otherwise, if the macro is included, the code segment will be executed.

[0159] The above code can be included in a macro, as follows:

[0160] #ifdef Thread_Count

[0161] All the code for ThreadRecod mentioned above

[0162] #else

[0163] Otherwise, if the macro is not defined, then an empty implementation needs to be provided.

[0164] Class ThreadRecod(){

[0165] Since the Void Dispatch(){} function will be called by subsequent code, an empty implementation must be provided.

[0166] };

[0167] Here we can see that the class has no member variables and only one empty function.

[0168] #endif

[0169] The advantages of the above technical solution are as follows: By placing the thread recording function in the base class method, the method, system, device, and storage medium for tracking task thread scheduling and switching proposed in this embodiment of the invention have extremely high versatility, are applicable to all system tasks, and provide developers with extremely convenient operation, greatly reducing the workload that developers need to pay attention to or develop. At the same time, the method of placing the thread recording function in the base class proposed in this embodiment allows the system to use the thread recording function in debug mode, while easily disabling this function in the official release, effectively improving the operational convenience and controllability of the thread recording function, and providing great convenience for developers.

[0170] In one embodiment of the present invention, step 5 is accomplished in two ways, specifically:

[0171] Method 1: As described in step 5, configure the execution of asynchronous tasks and thread delivery. Utilize the execution of asynchronous tasks to automatically perform thread switching statistics, including:

[0172] Step 501a: Based on the number of functions required to complete the task, add an asynchronous task execution function to each function; the asynchronous task execution function has the function of detecting the execution of the current thread and can automatically complete the statistical function of thread switching.

[0173] Step 502a: Call the Dispatch() interface of the execution function of the asynchronous task, and the thread switching statistics will be automatically completed.

[0174] The working principle of the technical solution provided by Method 1 is as follows:

[0175] Based on the aforementioned basic components, each asynchronous task needs to follow certain rules during execution in order to track its execution.

[0176] Class Task public:ThreadRecod{

[0177] Assuming the task class is a Task class, it must inherit from the ThreadRecod class in order to have thread scheduling and statistics functions.

[0178] Now, assuming this task requires executing two functions to complete, then we need to add the following to each function:

[0179] Void Excute1(){This asynchronous task's execution function 1}

[0180] Since each function is an independent unit, and each thread must execute on a function-by-function basis, we only need to add a function to the execution body of this function to detect the execution of the current thread, and we can automatically complete the thread switching statistics function.

[0181] The Dispatch() function automatically handles thread switching statistics by simply calling the Dispatch() interface.

[0182] }

[0183] Void Excute2(){This asynchronous task's execution function 2}

[0184] Similarly, if this function has an interface 2 for executing functions, then the Dispatch() call also needs to be added to this interface 2.

[0185] }

[0186] In the Task class, determining which functions need to be called with Dispatch and which do not can be based on whether the function is declared as public or private in the Task. If it is private, it means that the function cannot be directly submitted to any asynchronous thread for execution, while only public functions can be submitted to asynchronous threads. Therefore, it is only necessary to add Dispatch to public functions.

[0187] }

[0188] The technical solution provided by Method 1 effectively improves the accuracy and efficiency of the statistical function that automatically completes thread switching.

[0189] Method 2, as described in step 5, involves setting up the execution of asynchronous tasks and thread delivery, utilizing the execution of asynchronous tasks to automatically complete thread switching statistics, including:

[0190] Step 501b: Write a script that scans all classes within the code during the execution of asynchronous tasks and thread delivery.

[0191] Step 502b: Scan all classes within the code during the execution of asynchronous tasks and thread dispatch, and check if they inherit from the ThreadRecod class. If they inherit from the ThreadRecod class, find the public methods of this class and automatically add a Dispatch() call to each method.

[0192] Step 503b scans all subclasses of the classes in the code to see if they inherit from the ThreadRecod class. If they inherit from the ThreadRecod class, it finds the public methods of this class and automatically adds a Dispatch() call to each method.

[0193] Step 504b: Execute the Dispatch() interface of the function, which will automatically complete the thread switching statistics.

[0194] The working principle of the technical solution provided by Method 2 is as follows:

[0195] By writing a script to inspect all the code, scanning all classes within the code, and checking if they inherit from the ThreadRecod class, and if so, finding the public methods of this class, and automatically adding a Dispatch() call to each method, the automatic addition function is achieved, eliminating the need to manually add it to each function.

[0196] At the same time, for each class, we need to find the subclasses of the subclasses to avoid omissions.

[0197] For example, class A: public class B; for example, class A inherits from class B.

[0198] Class B: public ThreadRecod. Since class B inherits from ThreadRecod, we need to add the Dispatch() call to the public methods of both class A and class B.

[0199] The technical solution provided by Method 2 has the following effects: it can effectively reduce the mechanical operations of developers, improve the convenience of operation for developers, and reduce development time.

[0200] This invention proposes a system for tracking thread scheduling and switching of tasks, such as... Figure 2 As shown, the system includes:

[0201] The thread creation module encapsulates a thread creation interface, which is used to create threads.

[0202] The thread name setting module is used to set the thread name for the thread created by the creation interface by using a map memory and setting a thread lock.

[0203] The thread detection module is used to detect whether the developer has used the thread creation interface to create a thread.

[0204] The switch determination module is used to set up a base class with thread data recording capabilities to determine whether the thread of the current task needs to be switched.

[0205] The statistics module is used to set the execution of asynchronous tasks and thread delivery, and to automatically complete the thread switching statistics by using the execution of asynchronous tasks;

[0206] The thread name setting module includes:

[0207] The mapping table forming module is used to map the correspondence between threads and thread names using the map storage to form a stored mapping table;

[0208] The thread lock setting module is used to set a thread lock;

[0209] The ID acquisition module is used to obtain the ID corresponding to the thread.

[0210] The input module is used to pass the thread name as a parameter using a lambda function;

[0211] The write module is used to write the mapThread variable to the thread using the write lock in the thread lock;

[0212] An execution module is used to store the thread name corresponding to the thread ID, and then execute the thread function of the thread, so that the thread begins to execute the corresponding task;

[0213] All the logic for setting the thread name is written in the macro #ifdef Thread_Count.

[0214] The thread detection module includes:

[0215] The call stack acquisition module is used to hook the system's thread creation using the hook function and obtain the call stack of thread creation.

[0216] The judgment module is used to determine whether there is a stack call to the createThread function in the call stack. If not, it means that the thread created by the developer was not created using the thread creation interface; if so, it means that the thread created by the developer was created using the thread creation interface.

[0217] The prompt module is used to output an error message when it is determined that the developer did not use the thread creation interface to create the thread, prompting the developer to use the thread creation interface to create the thread.

[0218] The switching determination module includes:

[0219] The recording module is used to set member variables and use the member variables to record the ID of the current thread;

[0220] A storage module is used to set up a linked list and use the linked list to store the thread IDs that the task has gone through in the order of the threads the task has gone through.

[0221] The constructor setting module is used to set the constructor, initialize the ID corresponding to the thread in the constructor, provide a scheduling interface for task switching, and thus record the thread of the task;

[0222] The destructor setting module is used to set destructors. Destructors indicate that the task will be consumed soon and that the task has been completed.

[0223] The output module is used to output all threads and their names during the execution of the task.

[0224] The constructor setting module process includes: current thread ID acquisition module 1; thread ID judgment module 1; and ID storage module 1.

[0225] The destructor setting module includes: current thread ID acquisition module 2; thread ID judgment module 2; and ID storage module 2.

[0226] The output module includes:

[0227] The thread reading module is used to read data simultaneously from multiple threads using a read lock.

[0228] The name reading module is used to read the corresponding thread name based on the mapping table stored in the map memory;

[0229] The output control module is used to write the thread name to a log file or print it out.

[0230] The statistics module includes two scheme formats:

[0231] The first form, such as Figure 2 As shown, the statistics module includes:

[0232] The switching function module is used to add an asynchronous task execution function to each function based on the number of functions required to complete the task; the asynchronous task execution function has the function of detecting the execution of the current thread and can automatically complete the statistical function of thread switching.

[0233] The automatic switching module, which calls the Dispatch() interface of the execution function of the asynchronous task, will automatically complete the thread switching statistics.

[0234] The second form, such as Figure 3 As shown, the statistics module includes:

[0235] A module is used to write a script that scans all code during task execution.

[0236] The "Find and Add Module 1" function scans all classes within the code during the execution of asynchronous tasks and thread dispatching, checking if they inherit from the ThreadRecod class. If they do, it searches for public methods of that class and automatically adds a call to Dispatch() to each method.

[0237] The second module for finding and adding is used to scan all subclasses of the classes in the code to see if they inherit from the ThreadRecod class. If they do inherit from the ThreadRecod class, the module finds the public methods of this class and automatically adds a call to Dispatch() to each method.

[0238] The automatic switching module, used in the Dispatch() interface of the line function, will automatically complete the thread switching statistics.

[0239] The above technical solution achieves the following effects: by naming threads with corresponding thread names, the execution status of threads during task execution can be accurately and clearly recorded, facilitating observation and understanding of the specific task execution process. Furthermore, the method, system, device, and storage medium for tracking task thread scheduling and switching proposed in this invention can accurately obtain the execution and execution order of all threads a task undergoes from start to finish, and accurately obtain the specific details of thread switching. With this thread switching information, task scheduling can be optimized, allowing tasks to be executed from beginning to end within a single thread as much as possible, thereby accelerating task execution.

[0240] A device for tracking thread scheduling switching of tasks, the device comprising:

[0241] Processing unit;

[0242] Memory configured to store instructions executable by a storage processing unit;

[0243] The processing unit is configured to implement any one of the methods described above during execution. The specific structure of the device, such as... Figure 4 As shown.

[0244] It includes at least one storage unit 11, at least one processing unit 12, and a bus 13 connecting different platform systems.

[0245] Storage unit 11 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 111 and / or a cache memory unit 112, and may further include a read-only memory unit (ROM) 113.

[0246] The storage unit 11 also stores a program product 2, which can be executed by the processing unit 12, causing the processing unit 12 to perform the steps of a thread scheduling and switching method for tracking tasks according to an embodiment of this application (such as...). Figure 1 (As shown). Storage unit 11 may also include a program / utility 114 having a set (at least one) of program modules 115, such program modules including but not limited to: operating system, one or more application programs, other program modules and program data, each or some combination of these examples may include an implementation of a network environment.

[0247] Accordingly, the processing unit 12 can execute the program product 2 described above, and can also execute the program / utility 114.

[0248] Bus 13 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, or a local bus using any of the various bus structures.

[0249] A device 1 for tracking thread scheduling and switching of tasks can also communicate with one or more external devices 14, such as a keyboard, pointing device, Bluetooth device, etc., and with one or more devices capable of interacting with the device 1, and / or with any device that enables the device 1 to communicate with one or more other computing devices (e.g., a router, modem, etc.). This communication can be performed via input / output (I / O) interface 15. Furthermore, the device 1 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 16. Network adapter 16 can communicate with other modules of the device 1 via bus 13. It should be understood that, although... Figure 3 As not shown, other hardware and / or software modules can be used in conjunction with device 1, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage platforms.

[0250] A non-transitory computer-readable storage medium, such as Figure 5 As shown, it includes: a storage medium storing computer-executable instructions, which, when executed by a processing unit, implement any of the methods described above. For details, see [link to documentation]. Figure 4 This invention also provides a computer-readable storage medium for thread scheduling and switching based on tracing tasks. This computer-readable storage medium stores a computer program that, when executed, implements the steps of a thread scheduling and switching method based on tracing tasks according to this invention (e.g., ...). Figure 1 (As shown). Figure 4 The present embodiment illustrates a program product 2 for implementing the above-described method, which may employ a portable compact disc read-only memory (CD-ROM) and include program code, and may run on a terminal device, such as a personal computer. However, the program product 2 of the present invention is not limited thereto. In the present invention, the readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device. The program product 2 may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disc read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof.

[0251] Computer-readable storage media may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable storage medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof. Program code for performing operations of the present invention may be written in any combination of one or more programming languages, including object-oriented programming languages ​​such as Java, C++, etc., and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on a user computing device, partially on a user device, as a standalone software package, partially on a user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing devices can be connected to user computing devices via any type of network, including local area networks (LANs) or wide area networks (WANs), or they can be connected to external computing devices (e.g., via the Internet using an Internet service provider).

[0252] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention also intends to include these modifications and variations.< / threadid>

Claims

1. A method of tracking thread scheduling switches for a task, the method comprising: The method for switching thread scheduling of the tracking task comprises: an interface for creating a thread is encapsulated, and a thread is created by using the interface for creating a thread; a thread name is set for the thread created by using the interface for creating a thread by using a map memory and a thread lock setting mode; it is detected whether a developer creates a thread by using the interface for creating a thread; a basic class with a thread data recording function is set, and it is determined whether the thread of a current task needs to be switched; execution of an asynchronous task and thread delivery are set, and switching statistics of the thread are automatically completed by using execution of the asynchronous task; wherein the thread name setting for the thread created by using the interface for creating a thread by using the map memory and the thread lock setting mode comprises: a map memory is used to map a corresponding relationship between a thread and a thread name, and a stored mapping table is formed; a thread lock is set; an id corresponding to the thread is obtained; the thread name is transmitted in a parameter form by using a lambda function the mapThread variable is written into the thread by using a write lock in the thread lock; the thread name corresponding to the id of the thread is stored, and then a thread function of the thread is executed, and the thread starts to execute a corresponding task; and all logics of the thread name setting are written in a macro#ifdef Thread_Count.

2. The method of claim 1, wherein the thread scheduling switch is tracked for a task, and the detection of whether the developer creates a thread by using the interface for creating a thread comprises: a hook function is written, and a thread creation of a system is hooked by using a function of the hook function, and a call stack of the thread creation is obtained; it is judged whether there is a stack call of a createThread function in the call stack, if not, it is indicated that the thread created by the developer is not created by using the interface for creating a thread, and if yes, it is indicated that the thread created by the developer is created by using the interface for creating a thread; when it is determined that the developer does not create a thread by using the interface for creating a thread, error information is output, and the developer is prompted to create a thread by using the interface for creating a thread.

3. The method of claim 1, wherein the thread scheduling switch is tracked for a task, and the process of setting the basic class with the thread data recording function comprises: a member variable is set, and the id of a current thread is recorded by using the member variable; a linked list is set, and the thread ids experienced by a task are stored in the order of threads experienced by the task by using the linked list; a constructor is set, the id corresponding to the thread is initialized in the constructor, a scheduling interface of task switching is provided, and the thread of the task is recorded; a destructor is set, and the task is indicated to be consumed immediately by using the destructor, and it is indicated that the task has been executed completely; all threads and thread names in an execution process of the task are output; wherein the process of initializing the id corresponding to the thread in the constructor, providing the scheduling interface of task switching, and recording the thread of the task comprises: the id of a current thread is obtained; Determine whether the id of the current thread is equal to the id of the previous execution. If the id of the current thread is not equal to the id of the previous execution, store the id of the current thread and assign a new id. If the id of the current thread is equal to the id of the previous execution, it means that it is the same thread, and the id of the thread does not need to be stored. Use the new id as the current latest thread current_id, and store the thread current_id in a linked list to represent that the current task has a thread switching operation.

4. The method of claim 3, wherein the thread scheduling switch is tracked for a task, and Output all threads and thread names during the execution of the task. The process includes: Simultaneously read multiple threads using a read lock; Read the corresponding thread name according to the mapping table stored in the map memory; Write the thread name to a log file or print it out.

5. The method of claim 1, wherein the thread scheduling switch is tracked for a task, and Set the execution of asynchronous tasks and thread delivery, and automatically complete the thread switching statistics using the execution of asynchronous tasks, including: According to the number of functions required to complete the task, add an asynchronous task execution function in each function. The asynchronous task execution function has the function of detecting the execution of the current thread, and can automatically complete the thread switching statistics function; Call the Dispatch() interface of the asynchronous task execution function, and the thread switching statistics will be automatically completed.

6. The method of claim 1, wherein the thread scheduling switch is tracked for a task, and Set the execution of asynchronous tasks and thread delivery, and automatically complete the thread switching statistics using the execution of asynchronous tasks, including: Write a script to scan all classes in the code during the execution of asynchronous tasks and thread delivery, and check whether they inherit from the ThreadRecod class. If they inherit from the ThreadRecod class, find the public methods of the class and automatically add the call of Dispatch() in each method; Scan all subclasses of the classes in the code, check whether they inherit from the ThreadRecod class, and if they inherit from the ThreadRecod class, find the public methods of the class and automatically add the call of Dispatch() in each method; Call the Dispatch() interface of the execution function, and the thread switching statistics will be automatically completed.

7. A system for tracking thread scheduling switches of a task, the system comprising: The system includes: A thread creation module for encapsulating a thread creation interface to create a thread using the thread creation interface; A thread name setting module for setting the thread name for the thread created by the creation interface using a map memory and a thread lock setting method; A thread detection module for detecting whether the developer uses the thread creation interface to establish a thread; A switching determination module for setting a basic class with thread data recording function to determine whether the thread of the current task needs to perform a switching operation; A statistics module for setting the execution of asynchronous tasks and thread delivery, and automatically completing the thread switching statistics using the execution of asynchronous tasks; The thread name setting module includes: A mapping table forming module for mapping the correspondence between threads and thread names using a map memory to form a stored mapping table. A thread lock setting module, configured to set a thread lock; An id obtaining module, configured to obtain an id corresponding to the thread; A passing-in module, configured to pass in the thread name in the form of a parameter by using a lambda function; A writing module, configured to write the mapThread variable into the thread by using a write lock in the thread lock; An executing module, configured to store the thread name corresponding to the id of the thread, and then execute a thread function of the thread, so that the thread starts to execute a corresponding task; All the logic of setting the thread name is written in a macro #ifdef Thread_Count.

8. A device for tracking thread scheduling and switching of tasks, characterized in that, The device comprises: a processing unit; a memory configured to store executable instructions of the processing unit; wherein the processing unit is configured to implement the method of any one of claims 1-6 when executed.

9. A non-transitory computer-readable storage medium, comprising: The storage medium stores computer executable instructions, and the computer executable instructions implement the method of any one of claims 1-6 when executed by the processing unit. ​

Citation Information

Patent Citations

  • Automatic asynchronous handoff identification

    CN106030456A

  • Data processing method and apparatus and recording medium, for implementing a schedule managing mechanism when a context switch has occurred

    US6829766B1