Task recording method and device, storage medium and electronic device
Patent Information
- Application Number
- CN202110862586.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-29
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2041-07-29
AI Technical Summary
[0004]鉴于上述问题,本发明提供了一种任务记录方法及相关设备,主要目的在于解决目前对于任务的状态、任务句柄对象的数量和任务原对象的数量的记录时,需要多个不同的用于记录的对象并且需要进行加锁操作,导致内存空间占用多、实现任务记录功能时程序性能低的问题
[0032]By employing the above technical solutions, this invention provides a task recording method, apparatus, storage medium, and electronic device. Its main objective is to solve the problem that current methods for recording task status, the number of task handle objects, and the number of original task objects require multiple different recording objects and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality. This invention achieves task recording by manipulating a state variable when the task status changes, altering the state flag in the state variable (where the task status is either canceled or not canceled), and the state variable being a binary atomic variable; by adding or removing task handle objects, manipulating the state variable to change a first count, where the first count represents the number of task handle objects; and by adding or removing original task objects, manipulating the state variable to change a second count, where the second count represents the number of original task objects. In the above scheme, by manipulating the state variables when the task state changes, and modifying the state flag in the state variables, the process of marking and storing the task state can save memory space and eliminate the need for locking operations, making it convenient to record the task state. By manipulating the state variables when adding or removing task handle objects, and modifying the first count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of task handle objects. By manipulating the state variables when adding or removing original task objects, and modifying the second count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of original task objects. The above solution enables the use of only one state variable to store three objects: the task status, the number of task handle objects, and the number of original task objects. This saves memory space used to store these three objects and allows for safe access and reading/writing of these three objects without locking operations in the case of multi-threaded access. This improves the performance of task recording and solves the problem that the current method of recording the aforementioned three objects requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality.
Smart Images

Figure CN115686814B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and in particular to a task recording method, apparatus, storage medium, and electronic device. Background Technology
[0002] To facilitate interaction between tasks, other tasks will query the status (cancelled, not cancelled) of a particular task to determine whether and how to execute these other tasks. Other tasks will hold the original task object of a particular task, which can be understood as a pointer to the task control block and the task execution block, in order to execute the task, cancel the task, or query the status of the task. Other tasks will also hold the task handle object of a particular task, which can be understood as a pointer to the task control block, in order to cancel the task or query the status of the task.
[0003] Currently, the records of task status, the number of task handle objects, and the number of original task objects are usually stored in multiple objects. When the task status changes, or when task handle objects or original task objects are added or deleted, different objects need to be operated on, which consumes a lot of memory space. Furthermore, in order for these objects to handle multi-threaded operations, these objects need to be locked, which leads to poor program performance. Summary of the Invention
[0004] In view of the above problems, the present invention provides a task recording method and related equipment. The main purpose is to solve the problem that when recording the status of a task, the number of task handle objects, and the number of original task objects, multiple different objects are required for recording and locking operations are required, resulting in high memory space consumption and low program performance when implementing the task recording function.
[0005] To address the aforementioned technical problems, in a first aspect, the present invention provides a task recording method, the method comprising:
[0006] When the task status changes, the status variables are manipulated to change the status flag in the status variables. The task status is either task canceled or task not canceled, and the status variables are binary atomic variables.
[0007] When adding or removing task handle objects, the state variable is manipulated to change the first count in the state variable, where the first count is the number of task handle objects;
[0008] When adding or removing task objects, the state variable is manipulated to change the second count in the state variable, where the second count is the number of task objects.
[0009] Optionally, before the steps of operating on the state variables, the method may further include:
[0010] Determine if the result of the bitwise AND operation between the state variable and the first variable is not zero;
[0011] If it is not zero, then operations can be performed on the state variable;
[0012] If the value is zero, then operations cannot be performed on the state variable;
[0013] The first variable is determined based on the storage location of the atomic tag bit in the state variable.
[0014] Optionally, before the steps of operating on the state variables, the method may further include:
[0015] Create a second variable to store the task variable obtained through atomic read operations. When the result of the bitwise AND operation between the second variable and the first variable is not zero, atomically compare the state variable and the second variable. If the state variable and the second variable are equal bit by bit, replace the state variable with the result of the bitwise AND OR operation between the second variable and the first variable so that the result of the bitwise AND operation between the state variable and the first variable is zero.
[0016] Optionally, the step of changing the first count in the state variable includes:
[0017] Replace the state variable with the result of the arithmetic addition of the state variable and the third variable, or replace the state variable with the result of the arithmetic subtraction of the state variable and the third variable, to change the first count in the state variable, wherein the third variable is determined according to the storage location of the first count in the state variable.
[0018] Optionally, after the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the third variable, the following steps are included:
[0019] Determine whether the state variable is less than the fourth variable, where the fourth variable is twice the third variable;
[0020] If the state variable is less than the fourth variable, then delete the state variable.
[0021] Optionally, the step of changing the second count in the state variable includes:
[0022] Replace the state variable with the result of the arithmetic addition of the state variable and the fifth variable, or replace the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, to change the second count in the state variable, wherein the fifth variable is determined according to the storage location of the second count in the state variable.
[0023] Optionally, after the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, the following steps are included:
[0024] Determine whether the state variable is less than the sixth variable, where the sixth variable is the result of the arithmetic addition of the third and fifth variables;
[0025] If the state variable is less than the sixth variable, then delete the state variable.
[0026] To achieve the above objectives, according to a second aspect of the present invention, a task recording device is provided, comprising:
[0027] The state recording unit is used to operate on the state variables and change the state flags in the state variables when the task state changes. The task state is either task canceled or task not canceled, and the state variables are binary atomic variables.
[0028] The first counting unit is used to manipulate the state variable when adding or removing task handle objects, changing the first count in the state variable, where the first count is the number of task handle objects;
[0029] The second counting unit is used to manipulate the state variable when the original task object is added or removed, thereby changing the second count in the state variable, where the second count is the number of original task objects.
[0030] To achieve the above objectives, according to a third aspect of the present invention, a storage medium is provided, the storage medium including a stored program, wherein, when the program is running, the device where the storage medium is located executes the steps of the task recording method of any one of the first aspects described above.
[0031] To achieve the above objectives, according to a fourth aspect of the present invention, an electronic device is provided, the electronic device including at least one processor and at least one memory connected to the processor; wherein the processor is configured to invoke program instructions in the memory to execute the steps of the task recording method as described in any of the first aspects.
[0032] By employing the above technical solutions, this invention provides a task recording method, apparatus, storage medium, and electronic device. Its main objective is to solve the problem that current methods for recording task status, the number of task handle objects, and the number of original task objects require multiple different recording objects and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality. This invention achieves task recording by manipulating a state variable when the task status changes, altering the state flag in the state variable (where the task status is either canceled or not canceled), and the state variable being a binary atomic variable; by adding or removing task handle objects, manipulating the state variable to change a first count, where the first count represents the number of task handle objects; and by adding or removing original task objects, manipulating the state variable to change a second count, where the second count represents the number of original task objects. In the above scheme, by manipulating the state variables when the task state changes, and modifying the state flag in the state variables, the process of marking and storing the task state can save memory space and eliminate the need for locking operations, making it convenient to record the task state. By manipulating the state variables when adding or removing task handle objects, and modifying the first count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of task handle objects. By manipulating the state variables when adding or removing original task objects, and modifying the second count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of original task objects. The above solution enables the use of only one state variable to store three objects: the task status, the number of task handle objects, and the number of original task objects. This saves memory space used to store these three objects and allows for safe access and reading / writing of these three objects without locking operations in the case of multi-threaded access. This improves the performance of task recording and solves the problem that the current method of recording the aforementioned three objects requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality. Attached Figure Description
[0033] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of exemplary embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:
[0034] Figure 1 This is a schematic flowchart illustrating a task recording method provided in Embodiment 1 of the present invention;
[0035] Figure 2 This is a schematic flowchart illustrating a task recording method provided in Embodiment 2 of the present invention;
[0036] Figure 3 This is a schematic flowchart illustrating a task recording method provided in Embodiment 3 of the present invention;
[0037] Figure 4 This is a schematic structural block diagram of a task recording device provided in Embodiment 4 of the present invention;
[0038] Figure 5 This is a schematic structural block diagram of an electronic device provided in Embodiment Six of the present invention. Detailed Implementation
[0039] Exemplary embodiments of the invention will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the invention are shown in the drawings, it should be understood that the invention may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this invention will be thorough and complete, and will fully convey the scope of the invention to those skilled in the art.
[0040] Example 1
[0041] To address the issues of high memory consumption and low program performance caused by the need for multiple different recording objects and locking operations in current task recording methods, Embodiment 1 of this invention provides a task recording method, such as... Figure 1 As shown, the method may include:
[0042] Step 101: When the task state changes, operate on the state variables to change the state flag in the state variables;
[0043] The task status is either cancelled or not cancelled, and the status variable is a binary atomic variable.
[0044] Specifically, the status flag is the first bit in the status variable. It is 0 when the task status is canceled and 1 when the task status is not canceled. The task status can be determined by bitwise AND operation. There is a status variable for each task, and this status variable is shared by all task handle objects and the original task object corresponding to that task.
[0045] It should be noted that existing technologies typically use a regular variable to mark and store the task status, which consumes a lot of memory. In order to handle the situation where multiple threads operate simultaneously, locking operations are required. However, in this embodiment, the first bit of the status variable is used to mark and store the task status in step 101, which can save memory space and eliminate the need for locking operations in the process of marking and storing the task status, making it convenient to record the task status. Using bitwise operations can directly manipulate the status variable, which can save memory and make the process of judging the task status faster.
[0046] For example, the following kFlag variable can be designed to access task state data in the state variable:
[0047] std::uint64_t kFlag = 1;
[0048] Here, "std::uint64_t" indicates that "kFlag" is a 64-bit unsigned integer type, and "1" indicates that the task status is recorded in the first bit of the status variable.
[0049] It should be noted that the above code can be used to create the kFlag variable.
[0050] For example, the function to determine whether a task is cancelled or not can be implemented with the following code:
[0051] Bool result=(state&kFlag)! =0;
[0052] Here, "Bool" indicates that the returned result "result" is of Boolean type, and "state" is the state variable. When the bitwise AND operation between the state variable and the kFlag variable returns a non-zero result, the task status is determined to be the task not canceled. When the bitwise AND operation between the state variable and the kFlag variable returns a zero result, the task status is determined to be the task canceled.
[0053] It should be noted that bitwise AND operations can directly manipulate binary data, saving memory and making the process of determining task status faster. The code above can be used to determine whether a task is cancelled or not.
[0054] For example, the interface for querying task status can be implemented using the following code:
[0055] bool isCancellation()const noexcept{
[0056] std::uint64_t value=state_.load(std::memory_order_acquire);
[0057] }
[0058] Here, "isCancellation()" represents the interface name, "bool" indicates that the interface returns a boolean type, "noexcept" indicates the noexcept exception specification, "value" can be used to determine the current task status, and "std::memory_order_acquire" indicates that reads or writes in the current thread cannot be reordered to before this load, and all writes by other threads that release the same state variable can be seen by the current thread.
[0059] It should be noted that the code above implements an interface for querying task status, which can be used by either the task handle object or the original task object.
[0060] Step 102: When adding or removing task handle objects, manipulate the state variables to change the first count in the state variables;
[0061] The first count is the number of task handle objects.
[0062] Specifically, a task handle object can be understood as a pointer to a task control block, which can be used to query the task status or cancel the task; the third to thirty-third bits of the status variable can be used to store the first count.
[0063] It should be noted that existing technologies typically use a regular variable to record the number of stored task handle objects, which consumes a lot of memory and requires locking operations. In contrast, this embodiment uses the third to the thirty-third bits of the state variable to record the number of stored task handle objects in step 102, which saves memory space and eliminates the need for locking operations when recording the number of stored task handle objects, making it convenient to record the number of task handle objects.
[0064] For example, the following kHandleReference variable can be designed to access the first count in the state variable:
[0065] std::uint64_t kHandleReference=4;
[0066] Here, "std::uint64_t" indicates that "kHandleReference" is a 64-bit unsigned integer type, and "4" indicates that the first count is stored starting from the third bit of the state variable.
[0067] It should be noted that since the first count is stored starting from the third bit of the state variable, its value is 4 (represented as "100" in binary). The code above can create a variable to access the first count in the state variable.
[0068] For example, when the data stored in the third to thirty-third bits of the state variable is “0000000000000000000000000000000000001100”, it indicates that the number of task handle objects is 12.
[0069] Step 103: When adding or removing the original task object, operate on the state variable to change the second count in the state variable;
[0070] The second count is the number of original task objects.
[0071] Specifically, the original task object can be understood as a pointer to the task control block and the task execution block, which can be used to execute tasks, query task status, or cancel tasks; the 34th to 64th bits of the status variable can be used to store the second counter.
[0072] It should be noted that existing technologies typically use a regular variable to record the number of stored task handle objects, which consumes a lot of memory and requires locking operations. In contrast, this embodiment uses the 34th to 64th bits of the state variable to record the number of stored task handle objects in step 103. This saves memory space and eliminates the need for locking operations in the process of recording the number of stored task handle objects, making it convenient to record the number of original task objects.
[0073] For example, the following kSourceReference variable can be designed to access the second count in the state variable:
[0074] std::uint64_t kSourceReference=std::uint64_t(1)<<33u;
[0075] Among them, “std::uint64_t” indicates that “kSourceReference” is a 64-bit unsigned integer type, “std::uint64_t(1)<<33u” indicates that the second count is stored starting from the thirty-fourth bit of the state variable, “u” indicates that “33” is an unsigned number, and “<<” indicates a left shift operation.
[0076] For example, when the data stored in the 34th to 64th bits of the state variable is “0000000000000000000000000000000000010100”, it indicates that the number of task handle objects is 20.
[0077] By employing the above technical solution, this embodiment provides a task recording method, the main purpose of which is to solve the problem that current task recording requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing the task recording function. This embodiment of the invention achieves task recording by manipulating a state variable when the task state changes, modifying the state flag in the state variable, where the task state is either canceled or not canceled, and the state variable is a binary atomic variable; when adding or removing task handle objects, manipulating the state variable, modifying the first count in the state variable, where the first count is the number of task handle objects; and when adding or removing original task objects, manipulating the state variable, modifying the second count in the state variable, where the second count is the number of original task objects. In the above scheme, by manipulating the state variables when the task state changes, and modifying the state flag in the state variables, the process of marking and storing the task state can save memory space and eliminate the need for locking operations, making it convenient to record the task state. By manipulating the state variables when adding or removing task handle objects, and modifying the first count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of task handle objects. By manipulating the state variables when adding or removing original task objects, and modifying the second count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of original task objects. The above solution enables the use of only one state variable to store three objects: the task status, the number of task handle objects, and the number of original task objects. This saves memory space used to store these three objects and allows for safe access and reading / writing of these three objects without locking operations in the case of multi-threaded access. This improves the performance of task recording and solves the problem that the current method of recording the aforementioned three objects requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality.
[0078] Example 2
[0079] To enable those skilled in the art to more thoroughly understand the present invention and to fully convey the scope of the invention to them, Embodiment Two of the present invention, based on Embodiment One described above, further illustrates a task recording method defined by the present invention, such as... Figure 2 As shown, the method includes:
[0080] Step 201: When the task state changes, determine whether the result of the bitwise AND operation between the state variable and the first variable is not zero. If it is zero, perform an operation on the state variable to change the state flag in the state variable.
[0081] The first variable is determined based on the storage location of the atomic tag bit in the state variable.
[0082] Specifically, the atomic flag bit represents the atomic lock flag. When the atomic flag bit is 1, it means that a thread is writing to the state variable, the state variable is locked by an atomic lock, and it needs to wait for the atomic lock to be released. When the atomic flag bit is 0, it means that no thread is writing to the state variable, the state variable is not locked by an atomic lock, and it can be written. The first variable is the number of bits of the atomic flag bit stored in the state variable.
[0083] It should be noted that the atomic lock flag is used to indicate whether the current state variable can be written. Unlike conventional thread synchronization locks in existing technologies, such as mutexes or shared locks, using the atomic lock flag will greatly improve the performance of recording and modifying the task state, the number of task handle objects, and the number of original task objects. Bitwise operations can be used to directly manipulate the state variable, which can save memory and make the process of determining whether the current state variable can be written faster.
[0084] For example, the following kLocked variable can be designed as the first variable to access task state data in atomic variables:
[0085] std::uint64_t kLocked = 2;
[0086] Here, "std::uint64_t" indicates that "kLocked" is a 64-bit unsigned integer type, and "2" (represented as "10" in binary) indicates that the atomic marker bit is on the second bit of the state variable.
[0087] For example, the function to determine whether the current state variable can be written can be implemented by the following code:
[0088] Bool result=(state&kLocked)! =0;
[0089] Here, "Bool" indicates that the returned result "result" is of Boolean type, and "state" is the state variable. When the result of the bitwise AND operation between the state variable and the kLocked variable is not 0, it is determined that the current state variable cannot be written to. When the result of the bitwise AND operation between the state variable and the kLocked variable is 0, it is determined that the current state variable can be written to.
[0090] It should be noted that bitwise AND operations can directly manipulate binary data, saving memory and making the process of determining whether the current state variable can be written faster. The code above can be used to determine whether the current state variable can be written.
[0091] For example, an interface for determining whether the current state variable can be written can be implemented using the following code:
[0092] bool isLocked(){
[0093] std::uint64_t value=state_.load(std::memory_order_relaxed);
[0094] }
[0095] Here, "isLocked()" represents the interface name, "bool" indicates that the interface returns a boolean type, "value" can be used to determine whether the current state variable can be written, and "std::memory_order_relaxed" indicates that there are no synchronization or order constraints, and only atomicity is required for this operation.
[0096] It should be noted that the above code implements an interface for determining whether the current state variable can be written, and this interface can be used by the task handle object or the original task object.
[0097] Optionally, before operating on the state variable, the process may further include: creating a second variable to store the task variable obtained through an atomic read operation; when the result of the bitwise AND operation between the second variable and the first variable is not zero, atomically comparing the state variable and the second variable; if the state variable and the second variable are equal bit by bit, then replacing the state variable with the result of the bitwise AND OR operation between the second variable and the first variable, so that the result of the bitwise AND operation between the state variable and the first variable is zero.
[0098] Specifically, the state variable is first read using the `.load()` operation and assigned to the second variable. Then, a `while` loop checks whether the current state variable can be written. If the current state variable cannot be written, it waits for a period of time. If the current state variable can be written, the `.compare_exchange_weak()` operation is used to atomically compare the state variable and the second variable. If the state variable and the second variable are equal bit by bit, the state variable is replaced with the result of the bitwise AND operation between the second variable and the first variable, so that the result of the bitwise AND operation between the state variable and the first variable is zero.
[0099] It should be noted that the .compare_exchange_weak() operation can quickly and accurately perform an atomic comparison of the state variable and the second variable. If the state variable and the second variable are equal bit by bit, the operation of the state variable is replaced by the result of the bitwise AND-OR operation between the second variable and the first variable. This avoids redundant code during development and makes the recording of task status, the number of task handle objects, and the number of original task objects faster and more efficient.
[0100] For example, the above optional steps can be implemented by the following interface code:
[0101]
[0102]
[0103] Where "lock" is the interface name, "oldState" is the second variable, and "std::uint64_t" is the value of the `lock` interface. The `oldState = state_.load(std::memory_order_relaxed);` function reads the state variable and assigns it to the second variable. The `while(isLocked(oldState))` loop iterates through the state variable to check if it can be written to, exiting the loop only when the atomic lock on the state variable is released. `Sleep();` waits for a period of time if the state variable cannot be written to. `oldState = state_.load(std::memory_order_relaxed);` updates the second variable, where `state_` is the state variable. `state_.compare_exchange_weak(oldState, oldState|kLocked, std::memory_order_acquire, std::memory_order_relaxed));` atomically compares the state variable and the second variable; if they are equal bitwise, the state variable is replaced with the result of a bitwise AND operation between the second and first variables. `oldState|kLocked` performs a bitwise AND operation between the second and first variables.
[0104] It should be noted that the code above demonstrates the interface used to implement the aforementioned optional steps.
[0105] Step 202: When adding or removing task handle objects, determine whether the result of the bitwise AND operation between the state variable and the first variable is not zero. If it is zero, perform an operation on the state variable to change the first count in the state variable.
[0106] It should be noted that the atomic lock flag is used to indicate whether the current state variable can be written. Unlike conventional thread synchronization locks in existing technologies, such as mutexes or shared locks, using the atomic lock flag will greatly improve the performance of recording and modifying the task state, the number of task handle objects, and the number of original task objects using the state variable. Using bitwise AND operations can directly manipulate binary data, which can save memory and make the process of recording the number of task handle objects in the state variable faster.
[0107] Step 203: When adding or removing the original task object, determine whether the result of the bitwise AND operation between the state variable and the first variable is not zero. If it is zero, perform an operation on the state variable and change the second count in the state variable.
[0108] It should be noted that the atomic lock flag is used to indicate whether the current state variable can be written. Unlike conventional thread synchronization locks in existing technologies, such as mutexes or shared locks, using the atomic lock flag will greatly improve the performance of recording and modifying the task state, the number of task handle objects, and the number of original task objects. Bitwise operations can be used to directly manipulate the state variable, which can save memory and make the process of recording the number of original task objects faster.
[0109] Based on the above technical solution, this second embodiment provides a task recording method. Its main purpose is to solve the problem that current task recording requires multiple different objects for recording and locking operations, leading to high memory consumption and low program performance when implementing task recording functionality. Furthermore, it improves the performance of recording and modifying task status, the number of task handle objects, and the number of original task objects under multi-threaded operation. This second embodiment, based on the first embodiment, adds the following step before operating on the state variable: determining whether the result of the bitwise AND operation between the state variable and a first variable is not zero. If it is not zero, the state variable can be operated on; if it is zero, the state variable cannot be operated on. The first variable is determined based on the storage location of the atomic marker bit in the state variable, thus realizing task recording. In the above solution, by determining whether the result of the bitwise AND operation between the state variable and the first variable is not zero, the performance of recording and modifying task status, the number of task handle objects, and the number of original task objects using the state variable is improved.
[0110] Example 3
[0111] To enable those skilled in the art to more thoroughly understand the present invention and to fully convey the scope of the invention to them, Embodiment 3 of the present invention, based on Embodiment 1, further describes a task recording method defined by the present invention, such as... Figure 3 As shown, the method includes:
[0112] Step 301: When the task state changes, operate on the state variables to change the state flag in the state variables;
[0113] The task status is either cancelled or not cancelled, and the status variable is a binary atomic variable.
[0114] Step 302: When adding or removing task handle objects, operate on the state variable, replace the state variable with the result of the arithmetic addition of the state variable and the third variable, or replace the state variable with the result of the arithmetic subtraction of the state variable and the third variable, so as to change the first count in the state variable.
[0115] The third variable is determined based on the storage location of the first count in the state variable, where the first count is the number of task handle objects.
[0116] Specifically, the third variable is the decimal number represented by the first bit of the storage location of the first count in the state variable. For example, when the first bit of the storage location of the first count in the state variable is the third bit, the third variable is 4 (represented as "100" in binary). Arithmetic addition and arithmetic subtraction between the state variable and the third variable are implemented by the atomic operation interfaces .fetch_add() and .fetch_sub().
[0117] It should be noted that, compared to existing technologies for recording and modifying the number of task handle objects, using the atomic operation interfaces .fetch_add() and .fetch_sub() to change the first count makes the recording and modification of the number of task handle objects by the state variable faster and more efficient.
[0118] For example, the third variable can be assigned a value using the following code:
[0119] std::uint64_t kHandleReference=4;
[0120] Here, "std::uint64_t" indicates that the third variable "kHandleReference" is a 64-bit unsigned integer type, and "4" indicates that the first count is stored starting from the third bit of the state variable.
[0121] It should be noted that since the first count is stored starting from the third bit of the state variable, its value is 4 (represented as "100" in binary). The above code can be used to assign a value to the third variable.
[0122] For example, when adding a task handle, the change to the first count is implemented by the following code:
[0123] state_.fetch_add(kHandleReference,std::memory_order_relaxed);
[0124] Here, "state_" represents the task variable, "kHandleReference" represents the third variable, ".fetch_add" means atomically replacing the current state variable with the result of the arithmetic addition of the state variable and the third variable, and "std::memory_order_relaxed" means a relaxed operation, i.e., without synchronization or order constraints.
[0125] It should be noted that the above code is used to increment the first count by one.
[0126] Optionally, before replacing the state variable with the result of the arithmetic subtraction of the state variable and the third variable, the following steps may be included: determining whether the state variable is less than the fourth variable, where the fourth variable is twice the third variable; if the state variable is less than the fourth variable, then deleting the state variable.
[0127] It should be noted that both the original task object and the task handle object hold a smart pointer `std::unique_ptr` pointing to the state variable. This smart pointer indicates that there will only be one instance of the state variable. Through the optional steps mentioned above, the deletion function of the smart pointer can be customized. When the first and second counts in the state variable are both zero, that is, when the number of task handle objects and the number of original task objects are both zero, the state variable is deleted. This improves the efficiency of state variable deletion, facilitates multiple objects holding pointers to the state variable, eliminates the need to consider synchronous and asynchronous operations of multi-threading, and greatly improves the performance of task recording. When the third variable is 4 (represented as "100" in binary), the fourth variable is 8 (represented as "1000" in binary). If the state variable is less than the fourth variable, that is, the first four bits of the state variable are "01xx" (in binary, 01xx is less than 1000, and x represents 0 or 1). If any one of the following sixty bits is 0, it means that the first count stored in the third to thirty-third bits of the state variable is "0000000000000000000000000000000000000001" (represented as "1" in decimal), and the second count stored in the thirty-fourth to sixty-fourth bits of the state variable is "00000000000000000000000000000000000000000000" (represented as "0" in decimal). That is, there are 0 original task objects and 1 task handle object. In this case, reducing the number of task handle objects will make both the number of task handle objects and the number of original task objects zero, so the state variable needs to be deleted. Therefore, if the state variable is less than the fourth variable, it means that reducing the number of task handle objects will make both the number of task handle objects and the number of original task objects zero, so the state variable is deleted.
[0128] For example, when reducing a task handle by one, the change to the first count is implemented by the following code:
[0129] auto oldState=state_.fetch_sub(kHandleReference,std::memory_order_acq_rel);
[0130] Here, "oldState" represents the state variable before reassignment, "state_" represents the task variable, "kHandleReference" represents the third variable, ".fetch_sub" means atomically replacing the current state variable with the result of the arithmetic subtraction of the state variable and the third variable, and "std::memory_order_relaxed" means that read, modify, and write operations with this memory order are both acquisition and release operations. The current thread's read or write memory cannot be rearranged before or after this storage. All writes by threads that release the same atomic variable are visible before the modification, and the modification is visible to other threads that acquire the same atomic variable.
[0131] It should be noted that the above code is used to decrement the first count by one.
[0132] For example, after the above code is executed, the following code can also be executed to determine whether the state variable is less than the fourth variable. If the state variable is less than the fourth variable, then the state variable is deleted:
[0133] if(oldState<(2*kHandleReference)){
[0134] Delete this;
[0135] }
[0136] Here, "kHandleReference" is the third variable, "2*kHandleReference" is twice the third variable, i.e., the fourth variable, and "delete this;" means to delete this state variable.
[0137] It should be noted that if oldState is less than the fourth variable, it means that after decrementing the first count, both the number of task handle objects and the number of original task objects are zero, so the state variable is deleted.
[0138] Step 303: When adding or removing the original task object, operate on the state variable by replacing the state variable with the result of the arithmetic addition of the state variable and the fifth variable, or by replacing the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, so as to change the second count in the state variable.
[0139] The second count is the number of original task objects, and the fifth variable is determined based on the storage location of the second count in the state variable.
[0140] Specifically, the fifth variable is the decimal number represented by the first bit of the storage location of the second count in the state variable, and its specific value is determined in a similar way to the third variable; the arithmetic addition and subtraction of the state variable and the fifth variable are implemented by the atomic operation interfaces .fetch_add() and .fetch_sub().
[0141] It should be noted that, compared to existing technologies for recording and modifying the number of original task objects, using the atomic operation interfaces .fetch_add() and .fetch_sub() to change the second count makes the recording and modification of the number of original task objects by the state variable faster and more efficient.
[0142] It should be noted that, compared to existing technologies for recording and modifying the number of task handle objects, using the atomic operation interfaces .fetch_add() and .fetch_sub() to change the first count makes the recording and modification of the number of task handle objects by the state variable faster and more efficient.
[0143] For example, the fifth variable can be assigned a value using the following code:
[0144] std::uint64_t kSourceReference=std::uint64_t(1)<<33u;
[0145] Among them, “std::uint64_t” indicates that the fifth variable “kSourceReference” is a 64-bit unsigned integer type, “std::uint64_t(1)<<33u” indicates that the second count is stored starting from the thirty-fourth bit of the state variable, “u” indicates that “33” is an unsigned number, and “<<” indicates a left shift operation.
[0146] It should be noted that since the second count is stored starting from the thirty-fourth bit of the state variable, its value is “std::uint64_t(1)<<33u” (binary representation is “1000000000000000000000000000000000000000, decimal representation is “2^33”). The above code can be used to assign a value to the fifth variable.
[0147] For example, when adding a new task object, the change to the second count is achieved by the following code:
[0148] state_.fetch_add(kSourceReference,std::memory_order_relaxed);
[0149] Here, "state_" represents the task variable, "kSourceReference" represents the fifth variable, ".fetch_add" means atomically replacing the current state variable with the result of the arithmetic addition of the state variable and the fifth variable, and "std::memory_order_relaxed" means a relaxed operation, i.e., without synchronization or order constraints.
[0150] It should be noted that the above code is used to increment the second count by one.
[0151] Optionally, before replacing the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, the following steps may be included: determining whether the state variable is less than the sixth variable, where the sixth variable is the result of the arithmetic addition of the third variable and the fifth variable; if the state variable is less than the sixth variable, then the state variable is deleted.
[0152] It should be noted that both the original task object and the task handle object hold a smart pointer `std::unique_ptr` pointing to the state variable. This smart pointer indicates that there will only be one instance of the state variable. Through the optional steps mentioned above, the deletion function of the smart pointer can be customized. When the second count in the state variable is zero at the same time, that is, when the number of original task objects and the number of task handle objects are both zero, the state variable is deleted. This improves the efficiency of state variable deletion, facilitates multiple objects holding pointers to the state variable, eliminates the need to consider synchronous and asynchronous operations of multi-threading, and greatly improves the performance of task recording.
[0153] For example, when reducing the original task object by one, the change to the second count is achieved by the following code:
[0154] auto oldState=state_.fetch_sub(kSourceReference,std::memory_order_acq_rel);
[0155] Here, "oldState" represents the state variable before reassignment, "state_" represents the task variable, "kHandleReference" represents the fifth variable, ".fetch_sub" means atomically replacing the current state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, and "std::memory_order_relaxed" means that read, modify, and write operations with this memory order are both acquisition and release operations. The current thread's read or write memory cannot be rearranged before or after this storage. All writes by threads that release the same atomic variable are visible before the modification, and the modification is visible to other threads that acquire the same atomic variable.
[0156] It should be noted that the above code is used to decrement the second count by one.
[0157] For example, after the above code is executed, the following code can also be executed to determine whether the state variable is less than the sixth variable. If the state variable is less than the sixth variable, then the state variable is deleted:
[0158] if(oldState<(kSourceReference+kHandleReference)){
[0159] Delete this;
[0160] }
[0161] Among them, "kSourceReference+kHandleReference" is the sixth variable, and "delete this;" means to delete this state variable.
[0162] It should be noted that if oldState is less than the sixth variable, it means that after decrementing the second count, both the original task object count and the task handle object count are zero, so the state variable is deleted.
[0163] Based on the above technical solutions, this third embodiment provides a task recording method. Its main purpose is to solve the problem that current task recording methods require multiple different objects for recording and locking operations, leading to high memory usage and low program performance. Furthermore, it improves the speed and efficiency of recording and modifying the number of task handle objects and original task objects using state variables. This third embodiment, building upon the first embodiment, specifically clarifies the operations of changing the first and second counts in the state variables. The first count is changed by replacing the state variable with the result of arithmetic addition or subtraction of the state variable and a third variable, and the second count is changed by replacing the state variable with the result of arithmetic addition or subtraction of the state variable and a fifth variable. This further improves the speed and efficiency of recording and modifying the number of task handle objects and original task objects using state variables.
[0164] Example 4
[0165] Furthermore, as a response to the aforementioned Figure 1To implement the method shown, Embodiment 4 of the present invention also provides a task recording device for implementing the aforementioned method embodiments. This device embodiment 4 corresponds to the aforementioned method embodiments. For ease of reading, this task recording device embodiment 4 will not repeat the details of the aforementioned method embodiments one by one, but it should be clear that the electronic device in this embodiment can correspondingly implement all the contents of the aforementioned method embodiments. For example... Figure 4 As shown, the device 40 includes: a status recording unit 401, a first counting unit 402, and a second counting unit 403, wherein,
[0166] The state recording unit 401 is used to operate on the state variables and change the state flag in the state variables when the task state changes. The task state is either task canceled or task not canceled, and the state variables are binary atomic variables.
[0167] The first counting unit 402 is used to operate on the state variable when adding or removing task handle objects, changing the first count in the state variable, where the first count is the number of task handle objects;
[0168] The second counting unit 403 is used to operate on the state variable when the original task object is added or removed, and change the second count in the state variable, where the second count is the number of original task objects.
[0169] By employing the above technical solution, this embodiment four provides a task recording device, the main purpose of which is to solve the problem that current task recording requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing the task recording function. This embodiment four achieves task recording by manipulating a state variable when the task state changes, changing the state flag in the state variable, where the task state is either canceled or not canceled, and the state variable is a binary atomic variable; when adding or removing task handle objects, manipulating the state variable changes a first count in the state variable, where the first count is the number of task handle objects; when adding or removing original task objects, manipulating the state variable changes a second count in the state variable, where the second count is the number of original task objects. In the above scheme, by manipulating the state variables when the task state changes, and modifying the state flag in the state variables, the process of marking and storing the task state can save memory space and eliminate the need for locking operations, making it convenient to record the task state. By manipulating the state variables when adding or removing task handle objects, and modifying the first count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of task handle objects. By manipulating the state variables when adding or removing original task objects, and modifying the second count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of original task objects. The above solution enables the use of only one state variable to store three objects: the task status, the number of task handle objects, and the number of original task objects. This saves memory space used to store these three objects and allows for safe access and reading / writing of these three objects without locking operations in the case of multi-threaded access. This improves the performance of task recording and solves the problem that the current method of recording the aforementioned three objects requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality.
[0170] Example 5
[0171] Embodiment 5 of the present invention provides a storage medium on which a program is stored, which, when executed by a processor, implements the following task recording method:
[0172] When the task status changes, the status variables are manipulated to change the status flag in the status variables. The task status is either task canceled or task not canceled, and the status variables are binary atomic variables.
[0173] When adding or removing task handle objects, the state variable is manipulated to change the first count in the state variable, where the first count is the number of task handle objects;
[0174] When adding or removing task objects, the state variable is manipulated to change the second count in the state variable, where the second count is the number of task objects.
[0175] Optionally, before the steps of operating on the state variables, the method may further include:
[0176] Determine if the result of the bitwise AND operation between the state variable and the first variable is not zero;
[0177] If it is not zero, then operations can be performed on the state variable;
[0178] If the value is zero, then operations cannot be performed on the state variable;
[0179] The first variable is determined based on the storage location of the atomic tag bit in the state variable.
[0180] Optionally, before the steps of operating on the state variables, the method may further include:
[0181] Create a second variable to store the task variable obtained through atomic read operations. When the result of the bitwise AND operation between the second variable and the first variable is not zero, atomically compare the state variable and the second variable. If the state variable and the second variable are equal bit by bit, replace the state variable with the result of the bitwise AND OR operation between the second variable and the first variable so that the result of the bitwise AND operation between the state variable and the first variable is zero.
[0182] Optionally, the step of changing the first count in the state variable includes:
[0183] Replace the state variable with the result of the arithmetic addition of the state variable and the third variable, or replace the state variable with the result of the arithmetic subtraction of the state variable and the third variable, to change the first count in the state variable, wherein the third variable is determined according to the storage location of the first count in the state variable.
[0184] Optionally, after the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the third variable, the following steps are included:
[0185] Determine whether the state variable is less than the fourth variable, where the fourth variable is twice the third variable;
[0186] If the state variable is less than the fourth variable, then delete the state variable.
[0187] Optionally, the step of changing the second count in the state variable includes:
[0188] Replace the state variable with the result of the arithmetic addition of the state variable and the fifth variable, or replace the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, to change the second count in the state variable, wherein the fifth variable is determined according to the storage location of the second count in the state variable.
[0189] Optionally, after the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, the following steps are included:
[0190] Determine whether the state variable is less than the sixth variable, where the sixth variable is the result of the arithmetic addition of the third and fifth variables;
[0191] If the state variable is less than the sixth variable, then delete the state variable.
[0192] Example 6
[0193] Embodiment 6 of the present invention provides an electronic device 50, such as... Figure 5 As shown, the electronic device includes at least one processor 501 and at least one memory 502 connected to the processor; wherein the processor 501 is used to call program instructions in the memory 502 to execute the above-described task recording method.
[0194] By employing the above technical solution, this embodiment provides a task recording method and related equipment. The main purpose is to solve the problem that current task recording methods require multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance. This embodiment of the invention achieves task recording by manipulating a state variable when the task state changes, altering the state flag in the state variable. The task state is either canceled or not canceled, and the state variable is a binary atomic variable. When adding or removing task handle objects, the state variable is manipulated, changing a first count, where the first count represents the number of task handle objects. When adding or removing original task objects, the state variable is manipulated, changing a second count, where the second count represents the number of original task objects. In the above scheme, by manipulating the state variables when the task state changes, and modifying the state flag in the state variables, the process of marking and storing the task state can save memory space and eliminate the need for locking operations, making it convenient to record the task state. By manipulating the state variables when adding or removing task handle objects, and modifying the first count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of task handle objects. By manipulating the state variables when adding or removing original task objects, and modifying the second count in the state variables, the process of recording and storing the number of task handle objects can save memory space and eliminate the need for locking operations, making it convenient to record the number of original task objects. The above solution enables the use of only one state variable to store three objects: the task status, the number of task handle objects, and the number of original task objects. This saves memory space used to store these three objects and allows for safe access and reading / writing of these three objects without locking operations in the case of multi-threaded access. This improves the performance of task recording and solves the problem that the current method of recording the aforementioned three objects requires multiple different objects for recording and locking operations, resulting in high memory consumption and low program performance when implementing task recording functionality.
[0195] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable process management device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable process management device, generate instructions for implementing the process... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0196] In a typical configuration, the device includes one or more processors (CPUs), memory, and a bus. The device may also include input / output interfaces, network interfaces, etc.
[0197] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and memory includes at least one memory chip. Memory is an example of a storage medium.
[0198] Storage media, including both permanent and non-permanent, removable and non-removable media, can be used to store information by any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, storage media does not include transient media, such as modulated data signals and carrier waves.
[0199] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0200] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0201] Computer program code for performing the operations of embodiments of the present invention can be written in one or more programming languages or a combination thereof. Programming languages include object-oriented programming languages—such as Java, Smalltalk, and C++—as well as conventional procedural programming languages—such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0202] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A task recording method, characterized in that, include: When the task status changes, the status variable is manipulated to change the status flag in the status variable. The task status is either a task canceled state or a task not canceled state. The status variable is a binary atomic variable. When adding or removing task handle objects, the state variable is manipulated to change the first count in the state variable, where the first count is the number of task handle objects; When adding or removing task objects, the state variable is manipulated to change the second count in the state variable, where the second count is the number of task objects; The step of changing the first count in the state variable includes: replacing the state variable with the result of an arithmetic addition of the state variable and a third variable, or replacing the state variable with the result of an arithmetic subtraction of the state variable and a third variable, to change the first count in the state variable, wherein the third variable is determined according to the storage location of the first count in the state variable; After the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the third variable, the method includes: determining whether the state variable is less than the fourth variable, wherein the fourth variable is twice the third variable; if the state variable is less than the fourth variable, then deleting the state variable. The step of changing the second count in the state variable includes: replacing the state variable with the result of an arithmetic addition of the state variable and the fifth variable, or replacing the state variable with the result of an arithmetic subtraction of the state variable and the fifth variable, to change the second count in the state variable, wherein the fifth variable is determined according to the storage location of the second count in the state variable; After the step of replacing the state variable with the result of the arithmetic subtraction of the state variable and the fifth variable, the method includes: determining whether the state variable is less than the sixth variable, wherein the sixth variable is the result of the arithmetic addition of the third variable and the fifth variable; if the state variable is less than the sixth variable, then deleting the state variable.
2. The method according to claim 1, characterized in that, Before the step of operating on the state variable, the method further includes: Determine whether the result of the bitwise AND operation between the state variable and the first variable is not zero; If it is not zero, then the state variable can be operated on; If the value is zero, then the state variable cannot be operated on; The first variable is determined based on the storage location of the atomic tag bit in the state variable.
3. The method according to claim 2, characterized in that, Before the step of operating on the state variable, the method further includes: A second variable is created to store the task variable obtained through an atomic read operation. When the result of the bitwise AND operation between the second variable and the first variable is not zero, the state variable and the second variable are atomically compared. If the state variable and the second variable are equal bit by bit, the state variable is replaced with the result of the bitwise AND OR operation between the second variable and the first variable, so that the result of the bitwise AND operation between the state variable and the first variable is zero.
4. A storage medium, characterized in that, The storage medium includes a stored program, wherein, when the program is executed, it controls the device where the storage medium is located to perform the steps of the task recording method as described in any one of claims 1 to 3.
5. An electronic device, characterized in that, The electronic device includes at least one processor and at least one memory connected to the processor; wherein the processor is configured to invoke program instructions in the memory to execute the steps of the task recording method as described in any one of claims 1 to 3.