Methods, devices, electronic equipment, and media for determining the state of a data queue

By setting available and identified storage units in the data queue, and combining the positional relationship between write pointers and read pointers, the problem of monitoring the full state of the data buffer is solved, achieving efficient state identification and resource conservation.

CN116340021BActive Publication Date: 2025-10-31GUOKE FOUNDATION STONE (CHONGQING) SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310180481.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-02-28
Publication Date
2025-10-31
Estimated Expiration
2043-02-28

AI Technical Summary

Technical Problem

In existing technologies, data buffers cannot accurately monitor whether they are full when only data is being written, and the state of the buffer cannot be determined when reading and writing occur simultaneously, resulting in wasted resources and low efficiency.

Method used

By setting available storage units and identifier storage units in the storage units of the data queue, and utilizing the positional relationship between the write pointer and the read pointer, combined with the Compare-and-Swap (CAS) algorithm, the state of being full is determined when the write pointer and the identifier storage unit are adjacent and the identifier storage unit is located at the end of the queue; when reading and writing occur simultaneously, the state of the queue being empty or full is determined by the positional relationship between the read pointer and the identifier storage unit.

Benefits of technology

It enables accurate identification of the full or empty state of the data queue during both write and read/write periods, reducing resource consumption and improving the efficiency of status monitoring and system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116340021B_ABST
    Figure CN116340021B_ABST
Patent Text Reader

Abstract

This disclosure relates to a method, apparatus, electronic device, and medium for determining the state of a data queue, applicable to vehicles or other electronic devices. The data queue is used for communication between multiple threads of the same process. The state determination method includes: setting available storage units and identifier storage units in the storage units of the data queue, wherein the identifier storage units are in a disabled state; obtaining a write pointer pointing to a first position of the storage unit and a second position of the identifier storage unit; and determining that the data queue is in a full state when the first position is adjacent to the second position, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue. This method can accurately identify whether the available storage units in the data queue are full, whether there is only data writing or simultaneous data reading and writing, thus meeting the state monitoring requirements.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of computer technology, and in particular to a method, apparatus, electronic device, and medium for determining the state of a data queue. Background Technology

[0002] In operating systems, multiple threads within a process can use buffers for data communication. For example, consider the second-generation robot operating system (ROS2) in vehicle operating systems. ROS2 is an open-source robot operating system, and due to its mature framework, most developers build upper-layer applications based on it. In ROS2, by creating data buffers, some threads (as data senders) write data to the buffers, and other threads (as data receivers) read the data from the buffers, thus achieving data communication between threads.

[0003] During data communication between threads, each thread needs to accurately determine the state of the data buffer before operating on it. Some related technologies determine the data buffer's state by checking the positions of the read and write pointers. However, in some cases, when the read and write pointers point to the same position, it's impossible to determine whether the data buffer is full or empty. Other methods determine the data buffer's state by obtaining the data status of each storage unit, but this requires monitoring each storage unit, consuming monitoring resources. Furthermore, research has revealed that current data buffer monitoring primarily focuses on dynamic processes of data production and consumption (e.g., single-producer-single-consumer or multi-producer-multi-consumer models). However, during periods when only data is being written to, there is a need to monitor whether the data buffer is full; yet, related technologies have not implemented state monitoring for this scenario. Summary of the Invention

[0004] To overcome the problems existing in related technologies, embodiments of this disclosure provide a method, apparatus, electronic device, and medium for determining the state of a data queue.

[0005] According to a first aspect of the present disclosure, a method for determining the state of a data queue is provided. The method includes: setting an available storage unit and an identifier storage unit in the storage units of the data queue, wherein the identifier storage unit is in a disabled state; obtaining a write pointer pointing to a first position in the storage unit and a second position in the identifier storage unit; and determining that the data queue is in a full state when the first position is adjacent to the second position, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue.

[0006] In some embodiments, the state determination method further includes: obtaining the third position pointed to by the read pointer; wherein the available storage unit corresponding to the third position has not been read; determining whether the data queue is empty or full based on the relationship between the first position, the second position and the third position; the empty state indicates that the data queue is empty when both read and write operations are present, and the full state indicates that the data queue is full when both read and write operations are present.

[0007] In some embodiments, the number of available storage units is k, where k is a positive integer; determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: determining that the data queue is empty when the first position and the third position are the same and data has been read; determining that the data queue is full when the first position and the second position are adjacent and the third position is separated from the second position by k-1 storage units; determining that the data queue is full when the second position and the third position are adjacent, the first position is at the end of the data queue, and the second position is at the beginning of the data queue; and determining that the data queue is full when the first position, the second position, and the third position are sequentially adjacent in forward order based on the read / write order.

[0008] In some embodiments, the second position of the identifier storage unit is movable or fixedly located at the end of the data queue. When the second position of the identifier storage unit is movable, one of the following situations applies: the second position moves along with the first position; during the movement, the identifier storage unit is located at the forward adjacent position of the write pointer, where the forward direction corresponds to the read / write order of the data queue; or, the second position moves along with the third position; during the movement, the identifier storage unit is located at the reverse adjacent position of the read pointer, where the reverse direction corresponds to the reverse of the read / write order of the data queue; or, the second position moves along with the first position, and upon reaching the end of the data queue, switches to moving along with the third position.

[0009] In some embodiments, the data queue described above is used for communication between multiple threads within the same process. The data queue is created based on an array, and the corresponding memory space is 2. nWhere n is a positive integer; when the data queue is not full, the first thread among the multiple threads that has the priority to preempt the write pointer performs a data write operation in the available storage unit corresponding to the first position based on the Compare-and-Swap (CAS) algorithm. After successfully performing the data write operation, the write pointer points to the next storage unit to be written based on the read-write order; when the data queue is not full, the third thread among the multiple threads that has not the priority to preempt the write pointer performs a data write retry operation based on the Compare-and-Swap algorithm until the data write operation for the storage unit to be written is successfully performed; when the data queue is not empty, the second thread among the multiple threads that has the priority to preempt the read pointer performs a data read operation in the available storage unit corresponding to the third position based on the Compare-and-Swap algorithm. After successfully performing the data read operation, the read pointer points to the next storage unit to be read based on the read-write order; when the data queue is not empty, the fourth thread among the multiple threads that has not the priority to preempt the read pointer performs a data read retry operation based on the Compare-and-Swap algorithm until the data read operation for the storage unit to be read is successfully performed.

[0010] In some embodiments, the state determination method further includes: allocating a corresponding reference counter for each of the plurality of threads, wherein the reference counter is used to obtain a first count of data writing by the write pointer and a second count of data reading by the read pointer called by the corresponding thread, and calculating an atomic accumulation result based on the first count and the second count; and determining the target thread that meets the reclamation conditions and the corresponding reclamation timing based on the atomic accumulation result.

[0011] In some embodiments, the state determination method further includes: when the second position of the identifier storage unit is movable, after the data queue is in a full state, when the read pointer is detected to have read data from the first storage unit, setting the first storage unit as the identifier storage unit, and the second position corresponding to the identifier storage unit moves with the movement of the read pointer. During the movement, the identifier storage unit is located at the opposite adjacent position of the read pointer, where the opposite direction is the reverse direction of the read / write order of the data queue.

[0012] In some embodiments, the state determination method further includes: determining that the data queue is in a read-empty state when the read pointer points to the end of the data queue and the identifier storage unit is located at the opposite adjacent position of the read pointer.

[0013] In some embodiments, the data queue described above is a strip queue or a circular queue.

[0014] According to a second aspect of the present disclosure, a data queue state determination apparatus is provided. The state determination apparatus includes a marking module, a position acquisition module, and a state determination module. The marking module is used to set available storage units and identifier storage units in the storage units of the data queue, wherein the identifier storage units are in a disabled state. The position acquisition module is used to acquire a first position of the storage unit pointed to by a write pointer and a second position of the identifier storage unit. The state determination module is used to determine that the data queue is in a full state when the first position is adjacent to the second position, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue.

[0015] According to a third aspect of the present disclosure, a vehicle is provided that stores a set of instructions, which are executed by the vehicle's operating system to implement the data queue state determination method provided in the first aspect of the present disclosure.

[0016] According to a fourth aspect of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing processor-executable instructions; the processor being configured to read the executable instructions from the memory and execute the executable instructions to implement the data queue state determination method provided in the first aspect of the present disclosure.

[0017] According to a fifth aspect of the present disclosure, a computer-readable storage medium is provided that stores computer program instructions thereon, which, when executed by a processor, implement the data queue state determination method provided in the first aspect of the present disclosure.

[0018] The technical solutions provided by the embodiments of this disclosure may include the following beneficial effects:

[0019] By setting two different types of storage units within the storage unit: one is an available storage unit that can be used for data writing and reading; the other is an identifier storage unit, which is in a prohibited state and cannot be used for data writing or reading. Instead, it serves as a status marker, returning a special flag value when the write or read pointer points to this identifier storage unit. For periods with only data writing or simultaneous data reading and writing, by monitoring whether the second position (serving as a status marker position) of the identifier storage unit is at the end of the data queue, and if the second position is at the end of the data queue and the write pointer points to an adjacent available storage unit corresponding to the first position, the system can accurately identify whether the available storage units in the data queue are full, thus meeting the status monitoring requirements. Furthermore, if the write pointer returns a special flag value after pointing to the next position, it can determine that the first position pointed to by the write pointer is an available storage unit adjacent to the second position and corresponding to the first position, allowing for data writing. This position monitoring process is easy to implement and consumes relatively few resources.

[0020] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description

[0021] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.

[0022] Figure 1 This is a flowchart illustrating a method for determining the state of a data queue according to an exemplary embodiment.

[0023] Figure 2A This is a schematic diagram illustrating a data queue in a full state according to an exemplary embodiment.

[0024] Figure 2B This is a schematic diagram illustrating a data queue in a full state according to another exemplary embodiment.

[0025] Figure 2C This is a schematic diagram illustrating a data queue in a full state according to yet another exemplary embodiment.

[0026] Figure 2D This is a schematic diagram illustrating a data queue in a full state according to yet another exemplary embodiment.

[0027] Figure 3 This is a flowchart illustrating a method for determining the state of a data queue according to another exemplary embodiment.

[0028] Figure 4AThis is a schematic diagram illustrating an empty data queue state according to an exemplary embodiment.

[0029] Figure 4B This is a schematic diagram illustrating an empty data queue state according to another exemplary embodiment.

[0030] Figure 5A This is a schematic diagram illustrating a full bar queue according to an exemplary embodiment.

[0031] Figure 5B This is a schematic diagram illustrating a full circular queue according to an exemplary embodiment.

[0032] Figure 5C This is a schematic diagram illustrating a full bar queue according to another exemplary embodiment.

[0033] Figure 5D This is a schematic diagram illustrating a full circular queue according to another exemplary embodiment.

[0034] Figure 5E This is a schematic diagram illustrating a full bar queue according to yet another exemplary embodiment.

[0035] Figure 5F This is a schematic diagram illustrating a full circular queue according to yet another exemplary embodiment.

[0036] Figure 6 This is a block diagram of a data queue state determination apparatus according to an exemplary embodiment.

[0037] Figure 7 This is a block diagram of a vehicle according to an exemplary embodiment.

[0038] Figure 8 This is a block diagram of an electronic device according to an exemplary embodiment. Detailed Implementation

[0039] The exemplary embodiments will now be described in detail with reference to the accompanying drawings.

[0040] It should be noted that the relevant embodiments and accompanying drawings are only for describing and illustrating exemplary embodiments provided by this disclosure, and not all embodiments of this disclosure, nor should this disclosure be understood to be limited to the relevant exemplary embodiments.

[0041] It should be noted that the terms "first," "second," etc., used in this disclosure are only used to distinguish different steps, devices, or modules. These terms do not represent any specific technical meaning, nor do they indicate any order or interdependence between them.

[0042] It should be noted that the terms “a,” “a plurality of,” and “at least one” used in this disclosure are illustrative rather than restrictive. Unless otherwise expressly indicated in the context, they should be understood as “one or more.”

[0043] It should be noted that the term "and / or" used in this disclosure is used to describe the relationship between related objects, and generally indicates that there are at least three relationships. For example, A and / or B can at least indicate: the existence of A alone, the existence of both A and B, and the existence of B alone.

[0044] It should be noted that the various steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Unless otherwise specified, the scope of this disclosure is not limited by the order in which the steps are described in the relevant embodiments.

[0045] It should be noted that all actions involving the acquisition of signals, information, or data in this disclosure are carried out in compliance with the relevant data protection laws and policies of the country where the location is situated, and with authorization from the owner of the relevant device.

[0046] Exemplary methods

[0047] Figure 1 This is a flowchart illustrating a method for determining the state of a data queue according to an exemplary embodiment.

[0048] Reference Figure 1 As shown, the data queue state determination method provided in this embodiment includes the following steps: S110, S120 and S130.

[0049] The state determination method of this embodiment can be applied to electronic devices with operating systems, including but not limited to: computers, mobile phones, smart robots, smartwatches, smart bracelets, vehicles, etc.

[0050] In step S110, an available storage unit and an identifier storage unit are set in the storage unit of the data queue, wherein the identifier storage unit is in a disabled state.

[0051] A data queue is a data structure used for data storage, and the data in a data queue is also called a queue element. A data queue is called an empty queue when there are no elements in it; writing data into a data queue is called the enqueue operation; reading data from a data queue is called the dequeue operation.

[0052] In the embodiments of this disclosure, the available storage unit is a storage unit capable of data writing and reading. The storage unit is identified as being in a disabled state, meaning data cannot be written to or read from it. Instead, it serves as a status marker; when the write pointer or read pointer points to this identified storage unit, a specially set flag value will be returned.

[0053] In some embodiments, the location of the identification storage unit can be fixed or movable. When the identification storage unit is movable, the identity of each storage unit in the data queue can be switched from an available storage unit to an identification storage unit. For example, when written data in an available storage unit is read out, that available storage unit can switch to the identity of an identification storage unit.

[0054] In some embodiments, the data queue is a circular queue. In other embodiments, the data queue is a strip queue. In some embodiments, the data queue can be a bounded queue, which means a data queue with a fixed size. In other embodiments, the data queue can be an unbounded queue, without a fixed size, where data can be directly added until it overflows.

[0055] In step S120, the first position of the write pointer pointing to the above-mentioned memory cell and the second position of the above-mentioned identifier memory cell are obtained.

[0056] Figure 2A This is a schematic diagram illustrating a data queue in a full state according to an exemplary embodiment. Figure 2B This is a schematic diagram illustrating a data queue in a full state according to another exemplary embodiment. Figure 2C This is a schematic diagram illustrating a data queue in a full state according to yet another exemplary embodiment. Figure 2D This is a schematic diagram illustrating a data queue in a full state, according to yet another exemplary embodiment. Figures 2A to 2D In the diagram, a single arrow indicates the write pointer, and a double arrow indicates the read pointer.

[0057] In step 120, the location of the obtained identifier storage unit is described as the second location. Figures 2A to 2D The location L2 is shown in the diagram; the location of the memory cell pointed to by the acquired write pointer is described as the first location L1. Memory cells are indicated using grid-filled cells.

[0058] During data writing, the write pointer may point to an available storage unit or an identified storage unit. Therefore, the first position encompasses both the locations of available and identified storage units. For available storage units, the write pointer can write data and, once the data writing is complete, points to the next storage unit. For identified storage units, after the write pointer points to that unit, no more data can be written, and a special flag value is returned. This special flag value differs from the stored data and is used to indicate the unique identity of the storage unit.

[0059] A write pointer is a pointer used to write data to a data queue. In some embodiments, the write pointer moves from one end of the empty data queue (e.g., the start end described herein) to the position of the first memory cell, performs a write operation on the pointed-to memory cell, and the result may be that the write operation is complete (e.g., for an available memory cell), or it may fail to write data and return a special flag value (e.g., for an identifier memory cell); then it moves to the next memory cell until the write pointer reaches the end of the data queue. In some embodiments, for a circular queue, after the write pointer reaches the end of the data queue, it will move back to the start end of the data queue using a position modulo operation; for a strip queue, after the write pointer reaches the end of the data queue, if the written data in a previously written memory cell is read out, resulting in an empty available memory cell, the write pointer can jump to the position corresponding to the empty available memory cell and write sequentially.

[0060] In step S130, if the first position is adjacent to the second position, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue, it is determined that the data queue is in a full state.

[0061] The write-full state refers to the following state: the write pointer writes data to all writable storage units (i.e., available storage units) in the data queue, regardless of whether the data written in the data queue has been read out.

[0062] For example, refer to Figure 2A and Figure 2C As shown, examples are given for a strip queue and a circular queue in their full-write state only during the write phase; refer to Figure 2B and Figure 2D As shown, examples are given of the full states of a bar queue and a circular queue during periods when both read and write operations are performed simultaneously.

[0063] exist Figure 2A and Figure 2B The example uses a bounded striped queue as a data queue. This data queue contains 6 storage units, 5 of which are available storage units and the other is an identification storage unit. Figure 2A In the illustrated full state, the write pointer has sequentially written data a, b, c, and d to the first four memory units; currently, the write pointer points to the first memory unit position L1, which is the second-to-last position in the strip queue, and data e has been written to that position. At this point, the second position L2, which indicates the location of the memory unit, is the end of the strip queue, i.e., the last position. The first and second positions are adjacent. Figure 2B In the illustrated full state, the read pointer reads data 'a' from the data queue and points to the next storage unit to prepare to read data 'b' (indicating the state before it has been read).

[0064] The write pointer will then point to the next memory location, i.e., the second location, and a special flag value will be returned accordingly.

[0065] exist Figure 2C and Figure 2D The example uses a circular queue as a data queue. This data queue contains 6 storage units, of which 5 are available storage units and the other is an identification storage unit.

[0066] exist Figure 2C In the illustrated full state, the write pointer has sequentially written data a, b, c, and d to the first four memory units; currently, the write pointer points to the first memory unit, L1, which is the second-to-last position in the circular queue, and data e has been written to that position. At this point, the second position, L2, which indicates the location of the memory unit, is the end of the circular queue, i.e., the last position. The first and second positions are adjacent. Figure 2D In the illustrated full state, the read pointer reads data a and data b from the data queue and points to the next storage unit to prepare to read data c (indicating the state before it has been read).

[0067] The write pointer will then point to the next memory location, i.e., the second location, and a special flag value will be returned accordingly.

[0068] Therefore, in this embodiment, step S130 can determine whether the first and second positions meet the conditions for a full state in the following ways: ① If the write pointer returns a special flag value after pointing to the next position of the first position, it is determined that the first position pointed to by the write pointer is an available storage unit adjacent to the second position and corresponding to the first position, and data is being written to it; ② It is detected that the position of the identified storage unit is at the end of the data queue. If both conditions ① and ② are met, it is determined that the data queue is in a full state. The position monitoring process corresponding to this method is easy to implement and consumes fewer resources.

[0069] In embodiments including steps S110 to S130, two different types of storage units are set in the storage unit: one type is an available storage unit that can be used for writing and reading data; the other type is an identifier storage unit that is in a disabled state and cannot be used for writing or reading data. Instead, it serves as a status identifier position. When the write pointer or read pointer points to the identifier storage unit, a special flag value will be returned. For periods where only data is written or periods where data is read and written simultaneously, by monitoring whether the second position of the identifier storage unit (serving as a status identifier position) is at the end of the data queue, and when the second position is at the end of the data queue and the write pointer points to the first position, which is adjacent to the second position, and the available storage unit corresponding to the first position is writing data, the status of whether the available storage units in the data queue are full can be accurately identified, thus meeting the status monitoring requirements.

[0070] Figure 3 This is a flowchart illustrating a method for determining the state of a data queue according to another exemplary embodiment.

[0071] In some embodiments, the data queue state determination method, in addition to the steps S110 to S130 described above, also includes the following steps: S310 and S320. For simplicity, in... Figure 3 Only steps S310 and S320 are shown in the diagram.

[0072] In step S310, the third position pointed to by the read pointer is obtained; wherein the available storage unit corresponding to the third position has not yet been read.

[0073] The third location pointed to by the read pointer can be either an available memory location or an identifier memory location. If the read pointer points to an available memory location, the available memory location corresponding to the third location has not yet been read, for example, refer to... Figure 2B and Figure 2D The state shown.

[0074] In step S320, based on the relationship between the first position, the second position, and the third position, it is determined whether the data queue is empty or full. The empty state indicates that the data queue is empty when both read and write operations are occurring simultaneously, while the full state indicates that the data queue is full when both read and write operations are occurring simultaneously.

[0075] Figure 4A This is a schematic diagram illustrating an empty data queue state according to an exemplary embodiment. Figure 4B This is a schematic diagram illustrating an empty data queue state according to another exemplary embodiment.

[0076] In some embodiments, the number of available storage units is k, where k is a positive integer. For example, k = 5 is used as an example.

[0077] Reference Figure 4A and Figure 4B As shown, for a strip queue or a circular queue, in step S320 above, determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: determining that the data queue is empty when the first position and the third position are the same and the data has been read.

[0078] In some embodiments, the same first position and third position can correspond to the following states: the identifier storage unit moves with the write pointer and the identifier storage unit is located in the forward adjacent position of the write pointer; or, the identifier storage unit moves with the read pointer and the identifier storage unit is located in the reverse adjacent position of the read pointer; here, forward means the same direction as the read-write order, and reverse means the opposite direction to the read-write order.

[0079] In other embodiments, the first position and the third position being the same can correspond to the following state: the identifier storage unit is fixedly disposed at the end of the data queue.

[0080] Since the data queue follows a first-in, first-out (FIFO) order, if the read pointer sequentially reads the data written by the write pointer and catches up with the write pointer, and the read pointer has already read the data written to the position it points to, then it is considered that all the data written in the data queue has been read, and the corresponding data queue is in an empty state. Figure 4A In Chinese, the reading and writing order is from left to right. Figure 4B In this context, the read and write order is clockwise. The overlapping position between the third position L3 pointed to by the read pointer and the first position L1 pointed to by the write pointer can correspond to the location of available memory units, such as... Figure 4A As shown; it can also correspond to the location of the identified storage unit, see reference. Figure 4B As shown, in this case, a special flag value will be returned after the write pointer or read pointer points to the location of the identified memory unit. Here in... Figure 4A and Figure 4B The diagrams illustrate two scenarios: a strip queue and a circular queue. It's understood that in an empty state of a strip queue, the overlapping position of the first and third positions can also be the second position corresponding to the identified storage unit; similarly, in an empty state of a circular queue, the overlapping position of the first and third positions can be any available storage unit. In some embodiments, the read / write order is the free memory order.

[0081] Figure 5A This is a schematic diagram illustrating a full bar queue according to an exemplary embodiment. Figure 5B This is a schematic diagram illustrating a full circular queue according to an exemplary embodiment.

[0082] Reference Figure 5A and Figure 5B As shown, for a strip queue or a circular queue, in step S320 above, determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: determining that the data queue is full when the first position is adjacent to the second position and the third position is separated from the second position by k-1 storage units.

[0083] For example, in Figure 5A and Figure 5B Taking k=5 as an example, when the first position L1 and the second position L2 are adjacent, and the third position L3 is separated from the second position L2 by 4 storage units, the data queue is full. It should be noted that... Figure 5B In this example, the interval between two positions is calculated based on the reading order, which corresponds to a clockwise direction.

[0084] Figure 5C This is a schematic diagram illustrating a full bar queue according to another exemplary embodiment. Figure 5D This is a schematic diagram illustrating a full circular queue according to another exemplary embodiment.

[0085] Reference Figure 5C and Figure 5D As shown, for a strip queue or a circular queue, in step S320 above, determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: determining that the data queue is full when the second position is adjacent to the third position, the first position is located at the end of the data queue, and the second position is located at the beginning of the data queue.

[0086] For example, refer to Figure 5C and Figure 5D As shown, when the second position L2 and the third position L3 are adjacent, the first position L1 is located at the end of the data queue, and the second position L2 is located at the beginning of the data queue, the data queue is full.

[0087] exist Figure 5C and Figure 5DIn the illustrated full data queue, the second position L2 corresponding to the identifier storage unit is at the beginning of the data queue. The third position L3 pointed to by the read pointer is located at the second position of the data queue, adjacent to the second position L2. This indicates that the write pointer has read the data (e.g., data a) previously written to the starting storage unit, and the starting position has switched from being a usable storage unit to being an identifier storage unit. The first position L1 pointed to by the write pointer is at the end of the data queue. In this case, the storage unit at the end position is a usable storage unit, and the write pointer can write data f to the storage unit at the first position L1.

[0088] Figure 5E This is a schematic diagram illustrating a full bar queue according to yet another exemplary embodiment. Figure 5F This is a schematic diagram illustrating a full circular queue according to yet another exemplary embodiment.

[0089] Reference Figure 5E and Figure 5F As shown, for a strip queue or a circular queue, in step S320 above, determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: determining that the data queue is full when the first position, the second position, and the third position are adjacent in a forward order based on the read / write order.

[0090] exist Figure 5E In the illustrated full state of the bar queue, the nearest read pointer to the identified storage cell moves with the read pointer. The second position L2 corresponding to the identified storage cell is the second to last position in the data queue, and the third position L3 pointed to by the read pointer is at the end of the data queue. The read pointer has read the written data (e.g., data a, data b, data c, data d, and data e) from the storage cells from the beginning to the previous position. The storage cell at the second to last position switches from the identity of the available storage cell to the identity of the identified storage cell. At this time, the storage cell at the fourth position is written with new data j by the write pointer. Before this, the write pointer has already written the storage cells at the previous three positions. For example, the new data written in the example corresponds to data g, data h, and data i, respectively.

[0091] In some embodiments, as the read pointer continues to move to the right until it points to the end of the data queue, there are states where the data queues corresponding to the first, second, and third positions, which are sequentially adjacent based on the read-write order, are full. After the read pointer points to the end of the data queue and completes the read operation, the read pointer will jump to the beginning of the data queue to read the newly written data.

[0092] exist Figure 5FIn the illustrated full state of the bar queue, the nearest read pointer to the identified storage unit moves with the read pointer. The second position L2 corresponding to the identified storage unit is in the fourth position of the data queue, and the third position L3 pointed to by the read pointer is in the fifth position of the data queue. The read pointer has read the data previously written to the available storage units corresponding to the start and second positions (e.g., data a, data b, data c, and data d). The storage unit in the fourth position switches from the identity of an available storage unit to the identity of an identified storage unit. At this time, the storage unit in the third position is written with new data i by the write pointer. Before this, the write pointer has already written the storage units in the previous two positions. For example, the new data written in the example corresponds to data g and data h respectively.

[0093] In some embodiments, the second position of the aforementioned identifier storage unit is fixedly disposed at the end of the aforementioned data queue.

[0094] In other embodiments, the second location of the aforementioned identifier storage unit is movable.

[0095] In one embodiment, where the second position of the aforementioned identifier storage unit is movable, the second position moves in tandem with the movement of the first position. During this movement, the identifier storage unit is located in the forward adjacent position to the write pointer, where the forward direction corresponds to the read / write order of the data queue, for example, referring to... Figure 5A and Figure 5B The positional relationship between L1 and L2 is shown.

[0096] Another embodiment is that the second position moves in tandem with the third position; during the movement, the identifier storage unit is located in the reverse adjacent position of the read pointer, where the reverse direction is the opposite of the read / write order of the data queue, for example, referring to... Figures 5B to 5F The positional relationship between L2 and L3 is shown.

[0097] Another embodiment is: the second position moves in tandem with the first position, and upon reaching the end of the data queue, it switches to the second position moving in tandem with the third position, for example from... Figure 5A The corresponding position follows the state switch. Figure 5C The corresponding position follows the state.

[0098] In some embodiments, the state determination method described above further includes a setting step of switching the available storage unit to an identification storage unit.

[0099] Based on the embodiment including steps S210 to S230, or based on the embodiment including steps S210 to S230 and S310 to S320, the above state determination method further includes the following setting step: when the second position of the above-mentioned identifier storage unit is movable, after the above-mentioned data queue is in a full state, when it is detected that the read pointer has read the data of the first storage unit, the above-mentioned first storage unit is set as the above-mentioned identifier storage unit, and the second position corresponding to the above-mentioned identifier storage unit moves with the movement of the above-mentioned read pointer. During the movement, the above-mentioned identifier storage unit is located at the opposite adjacent position of the above-mentioned read pointer, and the opposite direction is the reverse direction of the read and write order of the above-mentioned data queue.

[0100] For circular queues, based on this setup step, the second location of the identified storage unit (corresponding to the physical address) does not need to be adjusted and automatically transitions from following the write pointer to following the read pointer.

[0101] For a strip queue, based on this setup step, after the location of the identified storage unit needs to be adjusted (jumping from the end of the queue to the beginning of the queue), the pointer is switched from following the write pointer to following the read pointer.

[0102] In some embodiments, the state determination method further includes the following step: when the read pointer points to the end of the data queue and the identifier storage unit is located at the opposite adjacent position of the read pointer, the data queue is determined to be in a read-empty state.

[0103] The read-empty state refers to the following state: the read pointer reads all the data in the available storage units of the data queue (e.g., the k available storage units in the previous example) in sequence, regardless of whether the empty storage units in the data queue have been rewritten.

[0104] Reference Figure 5E As shown, when the read pointer points to the end of the data queue and the identifier storage unit is located in the opposite direction to the read pointer, this state indicates that the data queue is empty. All previously written data corresponding to available storage units (e.g., 5 available storage units) has been read, regardless of whether any new data has been written to the empty storage units. Combined with... Figure 5A , Figure 5C and Figure 5E As shown, the read pointer reads data a, data b, data c, data d, and data e from the data queue in sequence.

[0105] In related technologies, for data buffers, in a multi-producer, multi-consumer model, data security is mostly ensured by locking mechanisms under multiple concurrent accesses. When one thread preempts a storage bit and holds the lock, regardless of whether the thread is actually performing an operation, other threads must wait for the lock to be released before they can read or write data to that storage bit by holding the lock. This may result in invalid waiting time, reducing the system's processing efficiency and performance.

[0106] In some embodiments, the data queue described above is used for communication between multiple threads within the same process. The data queue is created based on an array, and the corresponding memory space is 2. n , where n is a positive integer.

[0107] The aforementioned state determination method also includes the process of threads performing write and read operations, which can cover scenarios such as one or more write threads, one or more read threads, etc.

[0108] For example, in some embodiments, when the data queue is not full (the process of determining the data queue state can be implemented using the method of the aforementioned embodiments), the first thread among multiple threads in the same process that preempts the write pointer performs a data write operation in the available storage unit corresponding to the first position based on the Compare-and-Swap (CAS) algorithm. After successfully performing the data write operation, the write pointer points to the next storage unit to be written based on the read-write order.

[0109] In some embodiments, when the data queue is not full, the third thread among the multiple threads that did not preempt the write pointer performs a data write retry operation based on the CAS algorithm until the data write operation on the storage unit to be written is successfully performed.

[0110] In some embodiments, when the data queue is not empty, the second thread that preempts the read pointer in the same process performs a data read operation in the available storage unit corresponding to the third position based on the CAS algorithm. After successfully performing the data read operation, the read pointer points to the next storage unit to be read based on the read-write order.

[0111] In some embodiments, when the data queue is not empty, the fourth thread among the multiple threads that did not preempt the read pointer performs a data read retry operation based on the CAS algorithm until the data read operation on the storage unit to be read is successfully performed.

[0112] In the above embodiments, the CAS algorithm is used to write and read data from the data queue, reducing the invalid waiting time caused by locking. This is beneficial to improving the data processing efficiency of the operating system of electronic devices (such as the ROS2 system of a vehicle) in a multi-producer, multi-consumer mode. For example, the acquisition and distribution of vehicle camera data can be implemented using multiple threads within the same process. These threads communicate based on the aforementioned data queue and perform write and read operations based on the CAS algorithm, which can effectively improve the processing efficiency of camera data acquisition and distribution.

[0113] Furthermore, to overcome the ABA problem that arises during atomic operations based on the CAS algorithm (the ABA problem occurs when a thread U first reads shared memory data value A, then temporarily suspends for some reason, while another thread V temporarily changes the shared memory data value to B, and then changes it back to A. When the suspended thread U resumes and performs a CAS comparison, the final comparison result will be unchanged, thus passing the check, which is the ABA problem), a two-bit CAS algorithm can be used. One half of the bits is used as a pointer to store the expected value, and the other half is used as a counter to store the count value; the counter value is incremented by 1 for each operation. When comparing using the CAS algorithm, not only the actual value pointed to by the memory address and the expected value are compared, but also the consistency of the counter value is checked. This effectively avoids the ABA problem.

[0114] In some embodiments, the state determination method further includes: allocating a corresponding reference counter for each of the plurality of threads, wherein the reference counter is used to obtain a first count of data writing by the write pointer and a second count of data reading by the read pointer called by the corresponding thread, and calculating an atomic accumulation result based on the first count and the second count; and determining the target thread that meets the reclamation conditions and the corresponding reclamation timing based on the atomic accumulation result.

[0115] Some of the aforementioned threads may be non-core threads, created when the core threads lack sufficient processing power during data production and consumption in the data queue. Therefore, in order to maximize CPU resource utilization and ensure data security, and to reduce the CPU resource occupation of some idle threads, some target threads that meet the recycling criteria can be recycled at appropriate recycling times and reused as needed.

[0116] By assigning a corresponding reference counter to each thread, we can obtain the first count of data write operations and the second count of data read operations performed by each thread. For example, we can obtain the first and second counts based on the Fetch & ADD algorithm and calculate the atomic accumulation result. The atomic accumulation result is used to represent the processing volume of the corresponding thread. The larger the atomic accumulation result, the larger the processing volume of the corresponding thread. If the atomic accumulation result is always zero or always less than the set threshold within a preset time period, it can be regarded as meeting the recycling conditions. A certain moment within the preset time period can be used as the recycling opportunity.

[0117] Exemplary device

[0118] Figure 6 This is a block diagram of a data queue state determination apparatus according to an exemplary embodiment.

[0119] Reference Figure 6 As shown, the data queue status determination device 600 includes: a marking module 610, a position acquisition module 620, and a status determination module 630.

[0120] The aforementioned marking module 610 is used to set available storage units and identification storage units in the storage units of the aforementioned data queue, wherein the aforementioned identification storage units are in a prohibited state.

[0121] The aforementioned location acquisition module 620 is used to acquire the first location of the write pointer pointing to the aforementioned storage unit and the second location of the aforementioned identifier storage unit.

[0122] The aforementioned state determination module 630 is used to determine that the data queue is in a full state when the first position is adjacent to the second position, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue.

[0123] In some embodiments, the position acquisition module 620 is further configured to acquire the third position pointed to by the read pointer; wherein the available storage unit corresponding to the third position has not yet been read.

[0124] The aforementioned state determination module 630 is also used to determine whether the aforementioned data queue is empty or full based on the relationship between the aforementioned first position, the aforementioned second position, and the aforementioned third position.

[0125] In some embodiments, the state determination device 600 further includes a reference counter allocation module and a thread recycling determination module.

[0126] The reference counter allocation module described above is used to allocate a corresponding reference counter for each of the multiple threads. This reference counter is used to obtain the first count of data writing by the write pointer and the second count of data reading by the read pointer called by the corresponding thread, and to calculate an atomic accumulation result based on the first and second counts.

[0127] The aforementioned recycling determination module is used to determine the target thread that meets the recycling conditions and the corresponding recycling timing based on the above atomic accumulation results.

[0128] In some embodiments, the state determination device 600 further includes a transition setting module.

[0129] The aforementioned transition setting module is used to set the first storage unit as the aforementioned identifier storage unit when the second position of the aforementioned identifier storage unit is movable, after the aforementioned data queue is in a full state, and when the read pointer is detected to have read the data of the first storage unit, and the second position corresponding to the aforementioned identifier storage unit moves with the movement of the aforementioned read pointer. During the movement, the aforementioned identifier storage unit is located at the opposite adjacent position of the aforementioned read pointer, and the aforementioned opposite direction is the reverse direction of the read and write order of the aforementioned data queue.

[0130] In some embodiments, the state determination module 630 is further configured to determine that the data queue is in a read-empty state when the read pointer points to the end of the data queue and the identifier storage unit is located at the opposite adjacent position of the read pointer.

[0131] The specific details, beneficial effects, and other embodiments of the first embodiment can all be incorporated into this embodiment, and will not be repeated here.

[0132] Exemplary vehicle

[0133] Figure 7 This is a block diagram of a vehicle according to an exemplary embodiment.

[0134] Reference Figure 7 As shown, the vehicle 700 can be a gasoline-powered vehicle, a hybrid vehicle, an electric vehicle, a fuel cell vehicle, or other types of vehicles. The vehicle 700 may include multiple subsystems, such as a drive system 710, a control system 720, a sensing system 730, a communication system 740, an information display system 750, and a computing processing system 760. The vehicle 700 may also include more or fewer subsystems, and each subsystem may include multiple components, which will not be detailed here.

[0135] The drive system 710 includes components that provide power to the vehicle 700. These include, for example, an engine, an energy source, and a transmission.

[0136] The control system 720 includes components that provide control for the vehicle 700. These include, for example, vehicle control, cockpit equipment control, and driver assistance control.

[0137] The perception system 730 includes components that provide the vehicle 700 with perception of its surroundings. These include, for example, a vehicle positioning system, a laser sensor, a voice sensor, an ultrasonic sensor, and camera equipment.

[0138] The communication system 740 includes components that provide communication connectivity for the vehicle 700. These may include, for example, mobile communication networks (e.g., 3G, 4G, 5G networks), WiFi, Bluetooth, and vehicle-to-everything (V2X) connectivity.

[0139] The information display system 750 includes components that provide various information displays for the vehicle 700. These include, for example, vehicle information displays, navigation information displays, and entertainment information displays.

[0140] The computing processing system 760 includes components that provide data computing and processing capabilities for the vehicle 700. The computing processing system 760 may include at least one processor 761 and a memory 762. The processor 761 can execute instructions stored in the memory 762.

[0141] The processor 761 can be any conventional processor, such as a commercially available CPU. The processor may also include, for example, a Graphics Processing Unit (GPU), a Field Programmable Gate Array (FPGA), a System on Chip (SOC), an Application Specific Integrated Circuit (ASIC), or a combination thereof.

[0142] The memory 762 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk.

[0143] In this embodiment of the disclosure, a set of instructions is stored in the memory 762, and the processor 761 can execute the set of instructions to implement all or part of the steps of the state determination method described in any of the exemplary embodiments above.

[0144] Exemplary electronic devices

[0145] Figure 8This is a block diagram of an electronic device according to an exemplary embodiment.

[0146] Reference Figure 8 As shown, the electronic device 800 can be a vehicle controller, an in-vehicle terminal, an in-vehicle computer, or other types of electronic devices. The electronic device 800 may include at least one processor 810 and a memory 820. The processor 810 can execute instructions stored in the memory 820. The processor 810 is communicatively connected to the memory 820 via a data bus. In addition to the memory 820, the processor 810 can also be communicatively connected to an input device 830, an output device 840, and a communication device 850 via a data bus.

[0147] The processor 810 can be any conventional processor, such as a commercially available CPU. The processor may also include, for example, a Graphics Processing Unit (GPU), a Field Programmable Gate Array (FPGA), a System on Chip (SOC), an Application Specific Integrated Circuit (ASIC), or a combination thereof.

[0148] The memory 820 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk.

[0149] In this embodiment of the present disclosure, the memory 820 stores executable instructions, and the processor 810 can read the executable instructions from the memory 820 and execute the instructions to implement all or part of the steps of the state determination method described in any of the exemplary embodiments above.

[0150] Exemplary computer-readable storage media

[0151] In addition to the methods and apparatus described above, exemplary embodiments of this disclosure may also be a computer program product or a computer-readable storage medium storing the computer program product. The computer product includes computer program instructions that can be executed by a processor to perform all or part of the steps described in any of the methods in the exemplary embodiments described above.

[0152] The computer program product can be written in any combination of one or more programming languages ​​to perform the operations of the embodiments of this application. The programming languages ​​include object-oriented programming languages ​​such as Java and C++, as well as conventional procedural programming languages ​​such as C or similar languages, and scripting languages ​​(e.g., Python). The program code can be executed entirely on the user's computing device, partially on the user's device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0153] The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media include: static random access memory (SRAM) having one or more electrically connected wires, electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk, or any suitable combination thereof.

[0154] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of this disclosure. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the following claims.

[0155] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.

Claims

1. A method for determining the state of a data queue, characterized in that, The state determination method includes: The data queue's storage units are configured with available storage units and flag storage units, with the flag storage units being in a disabled state. For available storage units, the write pointer can write data and, once the data writing is complete, points to the next storage unit. For flag storage units, once the write pointer points to the flag storage unit, no data can be written, and a special flag value will be returned. Get the first location of the write pointer pointing to the memory cell and get the second location of the identifier memory cell; When the first position and the second position are adjacent, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue, the data queue is determined to be in a full state; the full state means that the write pointer writes data to all available storage units in the data queue, regardless of whether the data written in the data queue has been read out.

2. The state determination method according to claim 1, characterized in that, Also includes: Obtain the third location pointed to by the read pointer; wherein the available storage unit corresponding to the third location has not yet been read; Based on the relationship between the first position, the second position, and the third position, the data queue is determined to be either empty or full; the empty state indicates that the data queue is empty when both read and write operations are occurring simultaneously, and the full state indicates that the data queue is full when both read and write operations are occurring simultaneously.

3. The state determination method according to claim 2, characterized in that, The number of available storage units is k, where k is a positive integer; Determining whether the data queue is empty or full based on the relationship between the first position, the second position, and the third position includes: If the first position and the third position are the same and the data has been read, the data queue is determined to be empty. When the first position is adjacent to the second position and the third position is separated from the second position by k-1 storage units, the data queue is determined to be full. When the second position is adjacent to the third position, the first position is located at the end of the data queue, and the second position is located at the beginning of the data queue, the data queue is determined to be full. If the first position, the second position, and the third position are adjacent in a forward order based on the read / write order, the data queue is determined to be full.

4. The state determination method according to claim 2 or 3, characterized in that, The second position of the identifier storage unit is movable or fixedly located at the end of the data queue; When the second position of the identifier storage unit is movable, one of the following situations applies: The second position moves in tandem with the first position; during the movement, the identifier storage unit is located in the forward adjacent position of the write pointer, where the forward direction corresponds to the read / write order of the data queue; or, The second position moves in tandem with the movement of the third position; during the movement, the identifier storage unit is located at the position opposite to the read pointer, where opposite is the reverse direction corresponding to the read / write order of the data queue; or, The second position moves along with the first position, and when the end of the data queue is reached, the second position switches to move along with the third position.

5. The state determination method according to claim 2 or 3, characterized in that, The data queue is used for communication between multiple threads within the same process. The data queue is created based on an array, and its corresponding memory space is 2^32. n n is a positive integer; When the data queue is not full, the first thread among the multiple threads that preempts the write pointer performs a data write operation in the available storage unit corresponding to the first position based on the comparison and swap algorithm. After successfully performing the data write operation, the write pointer points to the next storage unit to be written based on the read and write order. When the data queue is not full, the third thread among the multiple threads that did not preempt the write pointer performs a data write retry operation based on the compare and swap algorithm until the data write operation on the storage unit to be written is successfully performed. When the data queue is not empty, the second thread among multiple threads that has the priority to preempt the read pointer performs a data read operation based on the comparison and swap algorithm in the available storage unit corresponding to the third position. After successfully performing the data read operation, the read pointer points to the next storage unit to be read based on the read and write order. When the data queue is not empty, the fourth thread among the multiple threads that did not preempt the read pointer performs a data read retry operation based on the compare-and-swap algorithm until the data read operation on the storage unit to be read is successfully performed.

6. The state determination method according to claim 5, characterized in that, Also includes: For each of the multiple threads, a corresponding reference counter is allocated. The reference counter is used to obtain the first count of data writing by the write pointer and the second count of data reading by the read pointer called by the corresponding thread, and to calculate the atomic accumulation result based on the first count and the second count. Based on the atomic accumulation results, the target thread that meets the recycling conditions and the corresponding recycling time are determined.

7. The state determination method according to claim 1, characterized in that, When the second position of the identifier storage unit is movable, after the data queue is full, when the read pointer reads the data of the first storage unit, the first storage unit is set as the identifier storage unit, and the second position corresponding to the identifier storage unit moves with the movement of the read pointer. During the movement, the identifier storage unit is located at the opposite adjacent position of the read pointer, where the opposite direction is the reverse direction of the read and write order of the data queue.

8. The state determination method according to claim 7, characterized in that, Also includes: If the read pointer points to the end of the data queue and the identifier storage unit is located in the opposite adjacent position to the read pointer, the data queue is determined to be in a read-empty state.

9. The state determination method according to any one of claims 1-3 and 6-8, characterized in that, The data queue is either a bar queue or a circular queue.

10. A device for determining the state of a data queue, characterized in that, The state determination device includes: The marking module is used to set available storage units and marked storage units in the storage units of the data queue. The marked storage units are in a disabled state. For available storage units, the write pointer can write data and points to the position of the next storage unit after the data writing is completed. For marked storage units, after the write pointer points to the marked storage unit, no data can be written and a special marked value will be returned. The location acquisition module is used to acquire the first location of the memory unit pointed to by the write pointer and to acquire the second location of the identifier memory unit; The state determination module is used to determine that the data queue is in a full state when the first position and the second position are adjacent, data is written to the available storage unit corresponding to the first position, and the second position is located at the end of the data queue; the full state means that the write pointer has written all the available storage units in the data queue with data, regardless of whether the data written in the data queue has been read out.

11. An electronic device, characterized in that, include: processor; Memory used to store the processor's executable instructions; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the state determination method according to any one of claims 1-9.

12. A computer-readable storage medium having computer program instructions stored thereon, characterized in that, When the program instructions are executed by the processor, they implement the state determination method according to any one of claims 1-9.

13. A vehicle, characterized in that, The vehicle stores a set of instructions, which are executed by the vehicle's operating system to implement the state determination method according to any one of claims 1-9.