Data operation method and device based on lock-free ring queue, equipment and medium
By allocating cache space in a lock-free circular queue to handle data lookup and update requests, the efficiency and consistency issues of data operations in a multi-threaded environment of traditional lock-free circular queues are solved, achieving efficient concurrent operations.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHONGQING CHANGAN AUTOMOBILE CO LTD
- Filing Date
- 2024-04-01
- Publication Date
- 2026-07-21
AI Technical Summary
Traditional lock-free circular queues have limitations in supporting parallel operations for data lookup and data update, and cannot effectively improve data operation efficiency and guarantee data consistency in a multi-threaded environment.
By dividing the preset cache space of the lock-free circular queue into the actual used cache space and the additional cache space, data lookup requests and data update requests are handled separately. Using the current head index and tail index as the boundary, it is ensured that data updates do not affect data lookup, thus achieving lock-free operation.
Without breaking the lock-free nature of the circular queue, it improves data operation efficiency and ensures data consistency, avoiding the overhead and complexity of locking mechanisms.
Smart Images

Figure CN118152435B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data manipulation technology, and in particular to a data manipulation method, apparatus, device and medium based on a lockless circular queue. Background Technology
[0002] In modern software engineering and computer systems, circular queues are a common data structure widely adopted due to their efficiency in memory utilization and data manipulation, especially in event-driven programming and message queue management. Lock-free circular queues are an improvement on traditional circular queues. They employ lock-free programming techniques, managing shared data through atomic operations, avoiding the overhead and complexity of using mutexes in multi-threaded environments. Lock-free data structures are particularly suitable for multi-core processor architectures because they can significantly reduce thread blocking and context switching, thereby improving system concurrency performance. However, traditional lock-free circular queues have limitations in supporting parallel operations for data lookup and update. Summary of the Invention
[0003] In view of this, this application aims to propose a data operation method, apparatus, device and medium based on a lock-free circular queue, which can improve data operation efficiency and ensure data consistency without breaking the lock-free characteristics of the circular queue.
[0004] A first aspect of this application provides a data operation method based on a lock-free circular queue, the method comprising:
[0005] Receive multiple data operation requests for a lock-free circular queue issued by multiple threads at the same time, wherein the data operation requests include at least data lookup requests and data update requests;
[0006] The data lookup request is responded to by the actual used cache space in the preset cache space of the lockless circular queue, and the data update request is responded to by the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
[0007] Optionally, before receiving multiple data operation requests for the lock-free circular queue issued by multiple threads at the same time, the method further includes:
[0008] Initialize the preset cache space of the lock-free circular queue. The size of the preset cache space is a first capacity. Allocate a second capacity of the actual used cache space and a third capacity of the additional cache space in the preset cache space according to the first capacity. The second capacity is used to represent the amount of data that can be stored in the actual used cache space. The third capacity is used to represent the amount of data that can be overflowed in the additional cache space. The first capacity is the amount of data that the preset cache space can hold. The first capacity is the sum of the second capacity and the third capacity.
[0009] Define member variables, including the current head index, the current tail index, a boolean variable, the currently used capacity, and the currently required storage capacity. The current head index points to the position of the latest data in the lock-free circular queue, the current tail index points to the position of the oldest data in the lock-free circular queue, the boolean variable indicates whether the actual used cache space of the lock-free circular queue is full, the currently used capacity indicates the amount of data that can be accommodated by the used capacity of the actual used cache space, and the currently required storage capacity is the current used capacity plus the amount of updated data corresponding to the data update request. The currently required storage capacity indicates the amount of data that the preset cache space of the lock-free circular queue needs to accommodate when responding to the data update request.
[0010] Optionally, responding to the data update request through additional cache space in the preset cache space of the lock-free circular queue includes:
[0011] The current header index is obtained through atomic operations, and the next header index is calculated based on the current header index and the memory order of the actual cache space used.
[0012] Obtain the timestamp corresponding to the updated data, and insert the updated data corresponding to the data update request and the timestamp corresponding to the updated data into the cache space pointed to by the next header index, wherein the timestamp and the updated data are stored in the form of key-value pairs;
[0013] The current header index is updated to the next header index through atomic operations, so that the latest data pointed to by the current header index is the updated data corresponding to the data update request.
[0014] Optionally, the method further includes:
[0015] When the current required storage capacity is less than the second capacity that can be stored in the actual used cache space, the value corresponding to the current used capacity is added to the number of updated data through an atomic operation so that the value of the current used capacity is the current required storage capacity;
[0016] When the current used capacity after performing the atomic operation plus 1 equals the second capacity that can be stored in the actual used cache space, it is determined that the actual used cache space is full of data, and the Boolean variable is set to logical true;
[0017] When the current required storage capacity is not less than the second capacity that can be stored in the actual used cache space, it is determined that the updated data will occupy the cache space of the data already stored in the actual used cache space;
[0018] After inserting the update data corresponding to the update request and the timestamp corresponding to the update data into the cache space pointed to by the next header index, and deleting the stored data and the timestamp corresponding to the stored data that were first stored in the preset cache space of the lock-free circular queue, the process includes:
[0019] The current tail index is updated based on the next head index, the third capacity, and the first capacity.
[0020] Optionally, the method further includes:
[0021] When the data lookup request indicates that the upper bound of the timestamp and the lower bound of the timestamp are to be retrieved in the lock-free circular queue, the upper bound of the timestamp and the lower bound of the timestamp are retrieved.
[0022] In the actual cache space, the upper bound of the timestamp is used as the first key to determine the first value corresponding to the first key, and the first value is used as the data of the upper bound of the timestamp.
[0023] In the actual cache space, the second value corresponding to the second key is determined using the lower bound of the timestamp as the second key, and the second value is used as the data of the lower bound of the timestamp.
[0024] Optionally, responding to the data lookup request through the actual used cache space in the preset cache space of the lock-free circular queue includes:
[0025] When the data lookup request indicates that the latest data in the lock-free circular queue is to be retrieved, the current head index is obtained through an atomic operation;
[0026] When the current header index is 0, the cache space for the latest data is determined to be the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1, and the latest data is obtained based on the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1.
[0027] When the current header index is not 0, the cache space of the latest data is determined to be the cache space pointed to by the current header index, and the latest data is obtained based on the cache space of the latest data.
[0028] Optionally, responding to the data lookup request through the actual used cache space in the preset cache space of the lock-free circular queue includes:
[0029] When the data lookup request indicates the need to retrieve the oldest data in the lock-free circular queue, the current tail index is obtained through an atomic operation.
[0030] The oldest data is determined based on the current tail index.
[0031] A second aspect of this application provides a data operation apparatus based on a lock-free circular queue, the apparatus comprising:
[0032] The receiving module is used to receive multiple data operation requests for the lock-free circular queue issued by multiple threads at the same time. The data operation requests include at least data lookup requests and data update requests.
[0033] The response module is used to respond to the data lookup request through the actual used cache space in the preset cache space of the lockless circular queue, and to respond to the data update request through the additional cache space in the preset cache space of the lockless circular queue.
[0034] The actual cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual cache space and the position of the actual cache space in the preset cache space of the lockless circular queue are dynamically changing.
[0035] A third aspect of this application provides an electronic device including a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the method described in the first aspect.
[0036] A fourth aspect of this application provides a computer-readable medium having a computer program / instructions stored thereon, which, when executed by a processor, implements the method described in the first aspect.
[0037] The beneficial effects of this application are:
[0038] This application provides a data operation method, apparatus, device, and medium based on a lock-free circular queue. It receives multiple data operation requests for the lock-free circular queue from multiple threads simultaneously. Each data operation request includes at least a data lookup request and a data update request. The data lookup request is responded to using the actually used cache space within the preset cache space of the lock-free circular queue, and the data update request is responded to using an additional cache space within the preset cache space of the lock-free circular queue. The actually used cache space and the additional cache space are distinguished by the current head index and the current tail index, respectively. The data update direction in the additional cache space is from the current head index to the current tail index. The positions of the actually used cache space and the preset cache space of the lock-free circular queue are dynamically changing.
[0039] By setting up an actual cache space for responding to data lookup requests and an additional cache space for responding to data update requests in the preset cache space of the lock-free circular queue, data update requests and data lookup requests are processed in different cache spaces. When there are data update requests and data lookup requests at the same time, the data updated in the additional cache space will not affect the data being looked up in the actual cache space. Thus, without breaking the lock-free characteristic of the circular queue, data operation efficiency is improved and data consistency is guaranteed.
[0040] The above description is only an overview of the technical solution of this application. In order to better understand the technical means of this application and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this application more obvious and understandable, the following are specific embodiments of this application. Attached Figure Description
[0041] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments of this application and their descriptions are used to explain this application and do not constitute an undue limitation of this application.
[0042] To more clearly illustrate the technical solution of this application, the drawings used in the description of this application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0043] Figure 1This is a flowchart of a data operation method based on a lock-free circular queue according to an embodiment of this application;
[0044] Figure 2 This is a schematic diagram of the structure of a lockless circular queue in a data operation method based on a lockless circular queue provided in an embodiment of this application;
[0045] Figure 3 This is a flowchart illustrating the response to a data update request in a data operation method based on a lock-free circular queue, provided in one embodiment of this application.
[0046] Figure 4 This is a schematic diagram of the framework of a lockless circular queue in a data operation device based on a lockless circular queue according to an embodiment of this application. Detailed Implementation
[0047] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other.
[0048] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0049] A lock-free circular queue is a data structure typically used for efficient data exchange in a multi-threaded environment. It enables high-performance concurrent operations without using locks. A lock-free circular queue usually consists of multiple slots, which can be read and written to by multiple threads simultaneously without locking. However, current lock-free circular queues only support lock-free operations, but not lock-free data lookup operations within the queue. If a data update request occurs during a data lookup, not only is the efficiency of data operations not improved, but data consistency in the lock-free circular queue cannot be guaranteed.
[0050] In view of the above-mentioned technical problems, this application proposes a data operation method, apparatus, device and medium based on a lock-free circular queue, so as to solve or partially solve the above-mentioned technical problems.
[0051] Figure 1 This is a flowchart illustrating a data operation method based on a lock-free circular queue, according to an embodiment of this application. Figure 2 This is a schematic diagram of the lock-free circular queue structure in a data operation method based on a lock-free circular queue according to an embodiment of this application. Figure 3This is a flowchart illustrating the response to a data update request in a data operation method based on a lock-free circular queue, as provided in an embodiment of this application. Figure 1 As shown in the figure, this embodiment provides a data operation method based on a lock-free circular queue, the method including:
[0052] In step S101, multiple data operation requests for the lock-free circular queue are received from multiple threads at the same time. The data operation requests include at least data lookup requests and data update requests.
[0053] In step S102, the data lookup request is responded to by the actual used cache space in the preset cache space of the lockless circular queue, and the data update request is responded to by the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
[0054] First, in step S101, multiple data operation requests for the lock-free circular queue are received from multiple threads at the same time. The data operation requests include at least data lookup requests and data update requests.
[0055] In this embodiment of the application, in a multi-threaded environment, multiple threads may simultaneously initiate multiple data operation requests to the lock-free circular queue. That is, each thread will initiate one or more data operation requests to the lock-free circular queue. The data operation requests include data lookup requests and data update requests. The data lookup request is to find data that has been stored in the lock-free circular queue, and the data update request is to add, delete, or modify data in the lock-free circular queue.
[0056] Furthermore, in step S102, the data lookup request is responded to by the actual used cache space in the preset cache space of the lockless circular queue, and the data update request is responded to by the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
[0057] In this embodiment of the application, the structure of the lock-free circular queue is as follows: Figure 2As shown, the preset cache space of the lock-free circular queue includes two parts: the actual cache space and the extra cache space. The actual cache space and the extra cache space are separated by the current head index and the current tail index in the preset cache space of the lock-free circular queue. The actual cache space responds to data lookup requests initiated by threads, while the extra cache space responds to data update requests initiated by threads. That is, the data responded to by the data lookup request and the data responded to by the data update request are in different cache spaces in the lock-free circular queue, which are independent of each other and do not affect each other. Therefore, when multiple threads operate in parallel, there is no need to lock the lock-free circular queue.
[0058] Furthermore, in the additional cache space, the direction of data updates in data update requests is from the current head index to the current tail index. The current head index points to the latest data already stored in the current lock-free circular queue. In actual use of the cache space, in the cache space where the current head index points to the current tail index, the data stored closer to the cache space pointed to by the current tail index is stored earlier, and the data stored closer to the cache space pointed to by the current head index is stored later.
[0059] It should be noted that, in this embodiment of the application, when data lookup requests and data update requests are processed simultaneously, since the data lookup requests and data update requests perform data operations in their respective independent cache spaces, updating data will not affect the data query, thereby ensuring data consistency during data lookup without the need for locking.
[0060] Through the above embodiments, by setting an actual cache space for responding to data lookup requests and an additional cache space for responding to data update requests in the preset cache space of the lockless circular queue, data update requests and data lookup requests are processed in different cache spaces. When there are data update requests and data lookup requests at the same time, the data updated in the additional cache space will not affect the data being looked up in the actual cache space. Thus, without breaking the lockless characteristic of the circular queue, the efficiency of data operation is improved and the consistency of data is guaranteed.
[0061] Optionally, before step S101, the method further includes:
[0062] like Figure 3As shown, the preset cache space of the lockless circular queue is initialized. The size of the preset cache space is a first capacity. The second capacity of the actual used cache space and the third capacity of the additional cache space are allocated according to the first capacity. The second capacity is used to represent the amount of data that can be stored in the actual used cache space. The third capacity is used to represent the amount of data that can be overflowed in the additional cache space. The first capacity is the amount of data that the preset cache space can hold. The first capacity is the sum of the second capacity and the third capacity.
[0063] Define member variables, including the current head index, the current tail index, a boolean variable, the currently used capacity, and the currently required storage capacity. The current head index points to the position of the latest data in the lock-free circular queue, the current tail index points to the position of the oldest data in the lock-free circular queue, the boolean variable indicates whether the actual used cache space of the lock-free circular queue is full, the currently used capacity indicates the amount of data that can be accommodated by the used capacity of the actual used cache space, and the currently required storage capacity is the current used capacity plus the amount of updated data corresponding to the data update request. The currently required storage capacity indicates the amount of data that the preset cache space of the lock-free circular queue needs to accommodate when responding to the data update request.
[0064] Specifically, in the above embodiments, before performing data operations on the lock-free circular queue, it is necessary to first initialize a lock-free circular queue. Based on the actual data operation requirements, a specified internal buffer space is received as the capacity of the lock-free circular queue, which is the preset buffer space of the lock-free circular queue, i.e., the first capacity. In actual settings, the first capacity of the preset buffer space of the lock-free circular queue needs to be larger than the capacity required by the actual data operation. For example, the capacity of the lock-free circular queue can be set to 1.2 times, 1.5 times, or larger than the capacity required by the actual data operation. The portion of the first capacity of the preset buffer space of the lock-free circular queue that exceeds the capacity required by the actual data operation is used as an additional buffer space, the size of which is the third capacity. The capacity of the lock-free circular queue that meets the actual data operation requirements is used as the actual used buffer space, the size of which is the second capacity. The additional buffer space is used to handle data overflow or maintenance operations. In other words, the first capacity of the preset buffer space of the lock-free circular queue is equal to the sum of the second capacity of the actual used buffer space and the third capacity of the additional buffer space.
[0065] In the embodiments of this application, the capacity of each cache space is actually the amount of data that can be accommodated. The second capacity is used to represent the amount of data that can be stored in the actual cache space. The third capacity is used to represent the amount of data that can be overflowed in the additional cache space. The first capacity is the amount of data that can be accommodated in the preset cache space. The first capacity is the sum of the second capacity and the third capacity.
[0066] As an example, the default cache space of the lockless circular queue has a first capacity of 120 data items, a second capacity of 100 data items in actual use, and a third capacity of 20 data items that can be accepted to overflow.
[0067] To initialize the sum of the capacities of each part in a lock-free circular queue, it is necessary to define member variables for the lock-free circular queue. Member variables include the current head index, the current tail index, a boolean variable, the current used capacity, and the current required storage capacity.
[0068] The current head index points to the position of the latest data in the lock-free circular queue, the current tail index points to the position of the oldest data in the lock-free circular queue, the boolean variable indicates whether the actual used cache space of the lock-free circular queue is full of data. When the boolean variable is true (logical true), it means that the actual used cache space of the lock-free circular queue is full of data. When the boolean variable is false (logical false), it means that the actual used cache space of the lock-free circular queue is not full of data. The currently used capacity indicates the amount of data that can be accommodated by the used capacity of the actual used cache space. The currently required storage capacity is the current used capacity plus the number of updated data corresponding to the data update request. The currently required storage capacity indicates the amount of data that the preset cache space of the lock-free circular queue needs to accommodate when responding to the data update request.
[0069] As an example, the second capacity of the actual cache space used is enough to store 100 data items. When 20 data items have already been stored, the current used capacity is 20. At this time, when a data update request needs to update one data item, the current required storage capacity is 20 + 1 = 21.
[0070] It should be noted that when updated data is added to the lock-free circular queue, it is first inserted into the extra cache space. After the updated data is stored, the cache space in the extra cache space that contains the updated data is allocated as part of the actual cache space. Based on the characteristics of the lock-free circular queue data structure, the free cache space in the actual cache space that is the same size as the cache space for the updated data will be allocated as part of the extra cache space to process other updated data.
[0071] Optionally, step S102 includes:
[0072] The current header index is obtained through atomic operations, and the next header index is calculated based on the current header index and the memory order of the actual cache space used.
[0073] Obtain the timestamp corresponding to the updated data, and insert the updated data corresponding to the data update request and the timestamp corresponding to the updated data into the cache space pointed to by the next header index, wherein the timestamp and the updated data are stored in the form of key-value pairs;
[0074] The current header index is updated to the next header index through atomic operations, so that the latest data pointed to by the current header index is the updated data corresponding to the data update request.
[0075] Specifically, in the embodiments of this application, such as Figure 3 As shown, taking updating a piece of data as an example, when it is necessary to respond to a data update request through the additional cache space in the preset cache space of the lock-free circular queue, the current head index (head) must first be obtained through the atomic operation: head_.load(std::memory_order_relaxed). Based on the current head index and the memory order of the actual cache space used by the lock-free circular queue (memory_order_relaxed), the position of the next head index is calculated through the atomic operation: (head+1)%total_size_, where total_size_ represents the first capacity of the preset cache space in the lock-free circular queue.
[0076] As an example, suppose the initial capacity of the preset cache space is 5, and the current head index is 3. Using an atomic operation, we calculate (3+1)%5 to obtain the next head index, which is 4. This indicates that the updated data should be inserted at index 4. If we calculate (4+1)%5 again, the result is 0, indicating that the lock-free circular queue inserted data at index 4 and then looped back to index 0, which conforms to the characteristics of a circular queue. This calculation method ensures that when updating data in a lock-free circular queue, the problem of not being able to add new data after the initial capacity is exhausted is avoided.
[0077] Furthermore, the timestamp corresponding to the update data that needs to be updated in the data update request is obtained. The timestamp of the update data is used to mark the time when the update data is inserted into the lock-free circular queue. The update data corresponding to the data update request and the timestamp corresponding to the update data are inserted into the cache space pointed to by the calculated next head index. The timestamp and the update data are stored in the cache space pointed to by the next head index in the form of key-value pairs.
[0078] After storing the updated data and its timestamp in the preset cache space of the lock-free circular queue, the current head index (head) is updated to the next head index (next_head) through the atomic operation: head_.store(next_head, std::memory_order_relaxed), so that the latest data pointed to by the updated current head index is the updated data corresponding to the data update request.
[0079] Optionally, the method further includes:
[0080] When the current required storage capacity is less than the second capacity that can be stored in the actual used cache space, the value corresponding to the current used capacity is added to the number of updated data through an atomic operation so that the value of the current used capacity is the current required storage capacity;
[0081] When the current used capacity after performing the atomic operation plus 1 equals the second capacity that can be stored in the actual used cache space, it is determined that the actual used cache space is full of data, and the Boolean variable is set to logical true;
[0082] When the current required storage capacity is not less than the second capacity that can be stored in the actual used cache space, it is determined that the updated data will occupy the cache space of the data already stored in the actual used cache space;
[0083] After inserting the update data corresponding to the update request and the timestamp corresponding to the update data into the cache space pointed to by the next header index, and deleting the stored data and the timestamp corresponding to the stored data that were first stored in the preset cache space of the lock-free circular queue, the process includes:
[0084] The current tail index is updated based on the next head index, the third capacity, and the first capacity.
[0085] Specifically, in the embodiments of this application, such as Figure 3 As shown, while responding to data update requests, it is necessary to update the currently used capacity in order to determine in real time whether the actual cache space used is full.
[0086] The following explanation assumes that only one piece of data was updated in response to the data update request. First, the currently used capacity (size) is obtained through the atomic operation: size_.load(std::memory_order_relaxed). Then, the relationship between the currently used capacity and the second capacity is determined. If the current required storage capacity is less than the second capacity that can be stored in the actual cache space, the number of updated data (i.e., the update data in the update request is 1) is added to the value corresponding to the currently used capacity through the atomic operation: size_.fetch_add(1,std::memory_order_relaxed) to make the value of the currently used capacity equal to the current required storage capacity.
[0087] Furthermore, after updating the value of the currently used capacity, it is necessary to further determine the relationship between the updated value of the currently used capacity and the second capacity that can be stored in the actual used cache space. When the current used capacity is incremented by 1 after the atomic operation and equals the second capacity that can be stored in the actual used cache space, it is determined that the actual used cache space is full of data, and the Boolean variable is set to logical true.
[0088] It should be noted that the count of currently used capacity starts from 0. Assuming the second capacity is 100, when the actual cache space is full of data, the count of used capacity is 99. Therefore, it is necessary to increment the count of used capacity by 1 to determine whether the actual cache space is full.
[0089] If it is determined that the current required storage capacity is not less than the second capacity that can be stored in the actual used cache space, it means that the actual used cache space is already full of data before storing the updated data. If the updated data needs to be inserted into the lock-free circular queue, the cache space of the already stored data needs to be overwritten. When the current required storage capacity is not less than the second capacity that can be stored in the actual used cache space, it is determined that the updated data will occupy the cache space of the already stored data in the actual used cache space.
[0090] Furthermore, the update data corresponding to the update request and the timestamp corresponding to the update data are inserted into the cache space pointed to by the next header index, and the stored data and the timestamp corresponding to the stored data are deleted from the preset cache space that was first stored in the lock-free circular queue.
[0091] As an example, assuming the second capacity is 100 and the count of used capacity is 99, and the number of update data to be stored is 1, then the current required storage capacity is 100, that is, the current required storage capacity is not less than the second capacity. If update data needs to be inserted at this time, the update data needs to be inserted into the extra cache space with its corresponding timestamp, and the stored data and the timestamps corresponding to the stored data that were first stored in the lock-free circular queue in the actual used cache space should be deleted to ensure that the second capacity of the actual used cache space is still 100.
[0092] Finally, based on the next head index (next_head), the third capacity (gap), and the first capacity (total_size), the current tail index (tail_) is updated using the atomic operation: tail_.store((next_head+gap)%total_size_,std::memory_order_relaxed).
[0093] Optionally, the method further includes:
[0094] When the data lookup request indicates that the upper bound of the timestamp and the lower bound of the timestamp are to be retrieved in the lock-free circular queue, the upper bound of the timestamp and the lower bound of the timestamp are retrieved.
[0095] In the actual cache space, the upper bound of the timestamp is used as the first key to determine the first value corresponding to the first key, and the first value is used as the data of the upper bound of the timestamp.
[0096] In the actual cache space, the second value corresponding to the second key is determined using the lower bound of the timestamp as the second key, and the second value is used as the data of the lower bound of the timestamp.
[0097] Specifically, in this embodiment, when a user needs to search for data stored at the start time (i.e., 11:00:01) and end time (i.e., 11:00:20) of a certain time period (e.g., 11:00:01-11:00:20), the data search request can represent the data at the upper bound of the timestamp and the data at the lower bound of the timestamp in the lock-free circular queue. The time of the upper bound of the timestamp is earlier than the time of the lower bound of the timestamp. The upper bound of the timestamp (i.e., 11:00:01) is the start time of the given time period, and the lower bound of the timestamp (11:00:01-11:00:20) is earlier than the time of the lower bound of the timestamp. 1:00:20) is the end time within a given time period. The corresponding value is searched in the actual cache space of the lock-free circular queue using the upper and lower bounds of the timestamp requested in the data lookup request as keys. This includes: determining the first value corresponding to the first key in the actual cache space using the upper bound of the timestamp as the first key, and using the first value as the data of the upper bound of the timestamp; determining the second value corresponding to the second key in the actual cache space using the lower bound of the timestamp as the second key, and using the second value as the data of the lower bound of the timestamp.
[0098] Optionally, step S102 includes:
[0099] When the data lookup request indicates that the latest data in the lock-free circular queue is to be retrieved, the current head index is obtained through an atomic operation;
[0100] When the current header index is 0, the cache space for the latest data is determined to be the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1, and the latest data is obtained based on the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1.
[0101] When the current header index is not 0, the cache space of the latest data is determined to be the cache space pointed to by the current header index, and the latest data is obtained based on the cache space of the latest data.
[0102] Specifically, in this embodiment, the data lookup request can also obtain the latest data in the lock-free circular queue through the atomic operation: head_.load(std::memory_order_relaxed), that is, the data in the cache space at the current head index position is the latest data.
[0103] When the current head index is determined, it is necessary to check whether the value of the current head index is 0. Taking a second capacity of 100 as an example, when the current head index is 0, it does not mean that the lock-free circular queue is empty, but rather that the latest data is stored in the cache space at index 99. The cache space for the latest data is determined to be the cache space pointed to by the value of the second capacity minus 1 that can be stored in the actual used cache space. The latest data is found based on the cache space pointed to by the value of the second capacity minus 1 that can be stored in the actual used cache space (i.e., the cache space at index 99). When the current head index is not 0, for example, when the current head index is 5, the cache space for the latest data is directly determined to be the cache space pointed to by the current head index. The latest data is obtained based on the cache space at index 5.
[0104] Optionally, step S102 includes:
[0105] When the data lookup request indicates the need to retrieve the oldest data in the lock-free circular queue, the current tail index is obtained through an atomic operation.
[0106] The oldest data is determined based on the current tail index.
[0107] Specifically, in this embodiment of the application, when a data lookup request indicates the need to retrieve the oldest data in a lock-free circular queue, the oldest data can be retrieved by using the atomic operation: tail_.load(std::memory_order_relaxed) to obtain the cache space pointed to by the current tail index (tail).
[0108] Figure 4 This is a schematic diagram of the framework of a lockless circular queue in a data manipulation device based on a lockless circular queue according to an embodiment of this application, as shown below. Figure 4 As shown, the device includes:
[0109] The receiving module 11 is used to receive multiple data operation requests for the lock-free circular queue issued by multiple threads at the same time. The data operation requests include at least data lookup requests and data update requests.
[0110] The response module 12 is used to respond to the data lookup request through the actual used cache space in the preset cache space of the lockless circular queue, and to respond to the data update request through the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
[0111] Optionally, the device further includes:
[0112] An initialization module is used to initialize a preset cache space of the lock-free circular queue before receiving multiple data operation requests for the lock-free circular queue issued by multiple threads at the same time. The size of the preset cache space is a first capacity. The module allocates a second capacity of the actual used cache space and a third capacity of the additional cache space in the preset cache space according to the first capacity. The second capacity is used to represent the amount of data that can be stored in the actual used cache space. The third capacity is used to represent the amount of data that can be overflowed in the additional cache space. The first capacity is the amount of data that the preset cache space can hold. The first capacity is the sum of the second capacity and the third capacity.
[0113] The member variable definition module is used to define member variables, including the current head index, the current tail index, a boolean variable, the currently used capacity, and the currently required storage capacity. The current head index points to the position of the latest data in the lock-free circular queue, the current tail index points to the position of the oldest data in the lock-free circular queue, the boolean variable indicates whether the actual used cache space of the lock-free circular queue is full, the currently used capacity indicates the amount of data that can be accommodated by the used capacity of the actual used cache space, and the currently required storage capacity is the current used capacity plus the amount of updated data corresponding to the data update request. The currently required storage capacity indicates the amount of data that the preset cache space of the lock-free circular queue needs to accommodate when responding to the data update request.
[0114] Optionally, the response module 12 includes:
[0115] The calculation module is used to obtain the current header index through atomic operations, and calculate the next header index based on the current header index and the memory order of the actual cache space used.
[0116] An insertion module is used to obtain the timestamp corresponding to the updated data, insert the updated data corresponding to the data update request and the timestamp corresponding to the updated data into the cache space pointed to by the next header index, wherein the timestamp and the updated data are stored in the form of key-value pairs;
[0117] The update module is used to update the current header index to the next header index through atomic operations, so that the latest data pointed to by the current header index is the updated data corresponding to the data update request.
[0118] Optionally, the device further includes:
[0119] The first execution module is used to add the number of updated data to the value corresponding to the currently used capacity through an atomic operation when the current required storage capacity is less than the second capacity that can be stored in the actual used cache space, so that the value of the currently used capacity is the current required storage capacity.
[0120] The second execution module is used to determine that the actual used cache space is full of data when the current used capacity plus 1 after the atomic operation is equal to the second capacity that can be stored in the actual used cache space, and to set the Boolean variable to logical true.
[0121] The third execution module is used to determine that the updated data will occupy the cache space of the data already stored in the actual cache space when the current required storage capacity is not less than the second capacity that can be stored in the actual cache space.
[0122] The fourth execution module is used to insert the update data corresponding to the update request and the timestamp corresponding to the update data into the cache space pointed to by the next head index, and delete the stored data and the timestamp corresponding to the stored data that were first stored in the preset cache space of the lock-free circular queue. Then, it updates the current tail index according to the next head index, the third capacity and the first capacity.
[0123] Optionally, the device further includes:
[0124] The timestamp acquisition module is used to acquire the upper and lower timestamp bounds when the data lookup request indicates that the upper and lower timestamp bounds in the lock-free circular queue are to be retrieved.
[0125] The first determining module is used to determine a first value corresponding to the first key in the actual used cache space, using the upper bound of the timestamp as the first key, and use the first value as the data of the upper bound of the timestamp;
[0126] The second determining module is used to determine a second value corresponding to the second key in the actual used cache space, using the lower bound of the timestamp as the second key, and to use the second value as the data of the lower bound of the timestamp.
[0127] Optionally, the response module 12 includes:
[0128] The current header index acquisition module is used to acquire the current header index through atomic operations when the data lookup request indicates that the latest data in the lock-free circular queue is to be retrieved.
[0129] The third determining module is used to determine, when the current header index is 0, the cache space of the latest data is the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1, and to obtain the latest data based on the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1;
[0130] The fourth determining module is used to determine the cache space of the latest data as the cache space pointed to by the current header index when the current header index is not 0, and to obtain the latest data based on the cache space of the latest data.
[0131] Optionally, the response module 12 includes:
[0132] The current tail index acquisition module is used to acquire the current tail index through atomic operations when the data lookup request indicates that the oldest data in the lock-free circular queue is to be retrieved.
[0133] The fifth determining module is used to determine the oldest data based on the current tail index.
[0134] Based on the same inventive concept, one embodiment of this application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps in the method described in the above embodiment.
[0135] Based on the same inventive concept, one embodiment of this application provides a computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements the steps in the method described in the above embodiments.
[0136] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.
[0137] For the sake of simplicity, the method embodiments are described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and components involved are not necessarily essential to this application.
[0138] Those skilled in the art will understand that embodiments of this application can be provided as methods, apparatus, or computer program products. Therefore, embodiments of this application can take the form of entirely hardware embodiments, entirely software embodiments, or embodiments combining software and hardware aspects. Furthermore, embodiments of this application can take the form of computer program products implemented 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.
[0139] This application describes embodiments with reference to flowchart illustrations and / or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of this application. It should 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 data processing terminal device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing terminal device, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0140] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing terminal device to operate in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0141] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal equipment, causing a series of operational steps to be performed on the computer or other programmable terminal equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable terminal equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0142] Although preferred embodiments of the present application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments of the present application.
[0143] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal device that includes said element.
[0144] The above provides a detailed description of a data operation method, apparatus, device, and medium based on a lock-free circular queue provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A data operation method based on a lock-free circular queue, characterized in that, The method includes: Receive multiple data operation requests for a lock-free circular queue issued by multiple threads at the same time, wherein the data operation requests include at least data lookup requests and data update requests; The data lookup request is responded to by the actual used cache space in the preset cache space of the lockless circular queue, and the data update request is responded to by the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
2. The data operation method based on a lock-free circular queue according to claim 1, characterized in that, Before receiving multiple data operation requests for the lock-free circular queue from multiple threads at the same time, the method further includes: Initialize the preset cache space of the lock-free circular queue. The size of the preset cache space is a first capacity. Allocate a second capacity of the actual used cache space and a third capacity of the additional cache space in the preset cache space according to the first capacity. The second capacity is used to represent the amount of data that can be stored in the actual used cache space. The third capacity is used to represent the amount of data that can be overflowed in the additional cache space. The first capacity is the amount of data that the preset cache space can hold. The first capacity is the sum of the second capacity and the third capacity. Define member variables, including the current head index, the current tail index, a boolean variable, the currently used capacity, and the currently required storage capacity. The current head index points to the position of the latest data in the lock-free circular queue, the current tail index points to the position of the oldest data in the lock-free circular queue, the boolean variable indicates whether the actual used cache space of the lock-free circular queue is full, the currently used capacity indicates the amount of data that can be accommodated by the used capacity of the actual used cache space, and the currently required storage capacity is the current used capacity plus the amount of updated data corresponding to the data update request. The currently required storage capacity indicates the amount of data that the preset cache space of the lock-free circular queue needs to accommodate when responding to the data update request.
3. The data operation method based on a lock-free circular queue according to claim 2, characterized in that, The step of responding to the data update request through the additional cache space in the preset cache space of the lockless circular queue includes: The current header index is obtained through atomic operations, and the next header index is calculated based on the current header index and the memory order of the actual cache space used. Obtain the timestamp corresponding to the updated data, and insert the updated data corresponding to the data update request and the timestamp corresponding to the updated data into the cache space pointed to by the next header index, wherein the timestamp and the updated data are stored in the form of key-value pairs; The current header index is updated to the next header index through atomic operations, so that the latest data pointed to by the current header index is the updated data corresponding to the data update request.
4. The data operation method based on a lock-free circular queue according to claim 3, characterized in that, The method further includes: When the current required storage capacity is less than the second capacity that can be stored in the actual used cache space, the value corresponding to the current used capacity is added to the number of updated data through an atomic operation so that the value of the current used capacity is the current required storage capacity; When the current used capacity after performing the atomic operation plus 1 equals the second capacity that can be stored in the actual used cache space, it is determined that the actual used cache space is full of data, and the Boolean variable is set to logical true; When the current required storage capacity is not less than the second capacity that can be stored in the actual used cache space, it is determined that the updated data will occupy the cache space of the data already stored in the actual used cache space; After inserting the update data corresponding to the update request and the timestamp corresponding to the update data into the cache space pointed to by the next header index, and deleting the stored data and the timestamp corresponding to the stored data that were first stored in the preset cache space of the lock-free circular queue, the process includes: The current tail index is updated based on the next head index, the third capacity, and the first capacity.
5. The data operation method based on a lock-free circular queue according to claim 3, characterized in that, The method further includes: When the data lookup request indicates that the upper bound of the timestamp and the lower bound of the timestamp are to be retrieved in the lock-free circular queue, the upper bound of the timestamp and the lower bound of the timestamp are retrieved. In the actual cache space, the upper bound of the timestamp is used as the first key to determine the first value corresponding to the first key, and the first value is used as the data of the upper bound of the timestamp. In the actual cache space, the second value corresponding to the second key is determined using the lower bound of the timestamp as the second key, and the second value is used as the data of the lower bound of the timestamp.
6. The data operation method based on a lock-free circular queue according to claim 4, characterized in that, The step of responding to the data lookup request through the actual used cache space in the preset cache space of the lock-free circular queue includes: When the data lookup request indicates that the latest data in the lock-free circular queue is to be retrieved, the current head index is obtained through an atomic operation; When the current header index is 0, the cache space for the latest data is determined to be the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1, and the latest data is obtained based on the cache space pointed to by the value of the second capacity that can be stored in the actual used cache space minus 1. When the current header index is not 0, the cache space of the latest data is determined to be the cache space pointed to by the current header index, and the latest data is obtained based on the cache space of the latest data.
7. The data operation method based on a lock-free circular queue according to claim 4, characterized in that, The step of responding to the data lookup request through the actual used cache space in the preset cache space of the lock-free circular queue includes: When the data lookup request indicates the need to retrieve the oldest data in the lock-free circular queue, the current tail index is obtained through an atomic operation. The oldest data is determined based on the current tail index.
8. A data manipulation device based on a lock-free circular queue, characterized in that, The device includes: The receiving module is used to receive multiple data operation requests for the lock-free circular queue issued by multiple threads at the same time. The data operation requests include at least data lookup requests and data update requests. The response module is used to respond to the data lookup request through the actual used cache space in the preset cache space of the lockless circular queue, and to respond to the data update request through the additional cache space in the preset cache space of the lockless circular queue. The actual used cache space and the additional cache space are distinguished by the current head index and the current tail index. The data update direction in the additional cache space is from the current head index to the current tail index. The position of the actual used cache space and the position of the actual used cache space in the preset cache space of the lockless circular queue are dynamically changing.
9. An electronic device, characterized in that, It includes a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the data manipulation method based on a lockless circular queue as described in any one of claims 1-7.
10. A computer-readable medium, characterized in that, It stores a computer program, wherein the computer program, when executed by a processor, implements the data operation method based on a lockless circular queue as described in any one of claims 1-7.