Single-writer multiple-reader data storage and reading method, device and apparatus
By comparing the read flag variable and the data read position variable in a single-writer, multi-reader system, the data contention problem of shared memory in multi-threaded systems is solved, and efficient resource utilization under lock-free operation is achieved.
Patent Information
- Application Number
- CN202111415550.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-25
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2041-11-25
AI Technical Summary
In existing technologies, data contention in multi-threaded shared memory leads to increased performance overhead, and common solutions such as locking operations consume computing resources.
By comparing the read flag variable and the data read position variable, it is determined whether other readers are reading data, thus resolving the data contention problem among multiple readers without locking, and adopting a single writer and multiple reader data storage and reading method.
This method resolves data contention between multiple read processes without locking, saving computing resources and improving resource utilization.
Smart Images

Figure CN114064301B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the field of data storage and reading, and specifically provides a single-writer multi-reader data storage and reading method, device and equipment. BACKGROUND
[0002] Modern computers mostly have multiple cores, and the software developed for computers uses multi-thread mode to develop software in order to effectively utilize multiple cores. Shared memory can meet the data exchange needs between threads.
[0003] However, the shared memory solution has the problem of data competition, and the key to improving the efficiency of this solution lies in how to solve the data competition. A common method to solve data competition is to use locking, but the locking operation will occupy part of the performance overhead. SUMMARY
[0004] The present disclosure provides a single-writer multi-reader data storage and reading method, device and equipment.
[0005] According to a first aspect of the present disclosure, a single-writer multi-reader data storage and reading method is provided. The method comprises:
[0006] The reading thread acquires the value of the read position atomic variable and the value of the read position variable in the array;
[0007] The read flag variable is set, and the value of the read flag variable is the value of the read position atomic variable;
[0008] The value of the read position atomic variable is increased by a preset size of the to-be-read data;
[0009] If the value of the read flag variable is the same as the value of the data read position variable in the array, the to-be-read data is read from the array based on the position pointed to by the data read position variable;
[0010] If the value of the read flag variable is not the same as the value of the data read position variable in the array, other reader threads are reading, the to-be-read data is not read from the array, and the value of the read atomic position variable after the value is increased is reduced by the preset size of the to-be-read data.
[0011] In some implementations of the first aspect, reading the preset to-be-read data from the array based on the position pointed to by the data read position variable comprises:
[0012] Acquiring the value of the write position variable in the array;
[0013] Determining the remaining read space of the array according to the value of the read position variable and the value of the write position variable;
[0014] if the remaining read space is greater than or equal to the space occupied by the data to be read, reading the data to be read from the array based on the position pointed to by the data read position variable.
[0015] In some implementations of the first aspect, determining the remaining read space of the array according to the value of the read position variable and the value of the write position variable comprises:
[0016] if the value of the write position variable is less than the value of the read position variable, the remaining read space is the sum of the capacity value of the array and the value of the write position variable minus the value of the read position variable;
[0017] if the value of the write position variable is greater than or equal to the value of the read position variable, the remaining read space is the value of the write position variable minus the value of the read position variable.
[0018] In some implementations of the first aspect, if the remaining read space is less than the space occupied by the data to be read, the data to be read is not read.
[0019] In some implementations of the first aspect, reading the preset data to be read from the array based on the position pointed to by the data read position variable further comprises:
[0020] when the array reads to the tail element position and the data to be read does not completely read the array, reading the part of the data to be read that has not read the array from the head element position of the array until the data of the size of the data to be read is read.
[0021] In some implementations of the first aspect, if the value of the read position variable plus the size of the data to be read is greater than the size of the array, the value of the read atomic position variable after the number is increased is subtracted by the size of the array.
[0022] In some implementations of the first aspect, further comprising a write thread, comprising:
[0023] determining the size of the writeable data according to the value of the read position variable and the value of the write position variable;
[0024] if the size of the writeable data is greater than the size of the expected write data, writing the data stored in the array based on the position pointed to by the write position variable and updating the value of the write position variable, wherein the size of the expected write data is the size of the data to be read minus the size of the remaining read space.
[0025] In some implementations of the first aspect, determining the size of the writeable data according to the value of the read position variable and the value of the write position variable comprises:
[0026] If the value of the write position variable is less than the value of the read position variable, the size of the writeable data is determined according to the value of the read position variable minus the value of the write position variable minus one;
[0027] If the value of the write position variable is greater than the value of the read position variable, the size of the writeable data is the sum of the capacity value of the array and the value of the read position variable minus the value of the write position variable minus one.
[0028] According to a second aspect of the present disclosure, there is provided a single-writer multiple-reader data storage and reading device, comprising:
[0029] An obtaining module is configured to cause a read thread to obtain the value of a read position atomic variable and the value of a read position variable in an array;
[0030] A setting module is configured to set a read flag variable, the value of the read flag variable being the value of the read position atomic variable;
[0031] A read position atomic variable adjusting module is configured to increase the value of the read position atomic variable by a preset size of data to be read;
[0032] A processing module is configured to, if the value of the read flag variable is the same as the value of the data read position variable in the array, read the data to be read from the array based on the position indicated by the data read position variable; if the value of the read flag variable is not the same as the value of the data read position variable in the array, another reader thread is reading, and the data to be read is not read from the array, and the value of the read atomic position variable after the increase is decreased by the preset size of the data to be read.
[0033] According to a third aspect of the present disclosure, there is provided an electronic device. The electronic device comprises a memory and a processor, the memory having stored thereon a computer program, the processor implementing the method as described above when executing the program.
[0034] The present disclosure determines whether other processes are reading data according to the value of the read flag variable and the value of the data read position variable, to solve the data race problem between multiple reading processes in the solution of shared memory without locking, thereby saving computing resources and improving resource utilization.
[0035] It should be understood that the content described in the summary section is not intended to limit or define key or important features of the embodiments of the present disclosure, nor is it used to limit the scope of the present disclosure. Other features of the present disclosure will become apparent from the following description. BRIEF DESCRIPTION OF DRAWINGS
[0036] The above and other features, advantages, and aspects of the embodiments of this disclosure will become more apparent from the accompanying drawings and the following detailed description. The drawings are provided for a better understanding of the invention and are not intended to limit the scope of this disclosure. In the drawings, the same or similar reference numerals denote the same or similar elements, wherein:
[0037] Figure 1 A flowchart illustrating a single-writer, multi-reader data storage and reading method according to an embodiment of the present disclosure is shown;
[0038] Figure 2 A schematic diagram of the reading process according to an embodiment of the present disclosure is shown;
[0039] Figure 3 A schematic diagram of the writing process according to an embodiment of the present disclosure is shown;
[0040] Figure 4 A block diagram of a single-writer, multi-reader data storage and reading apparatus according to an embodiment of the present disclosure is shown;
[0041] Figure 5 A block diagram of an electronic device for implementing the single-writer, multi-reader data storage and retrieval method of embodiments of the present disclosure is shown. Detailed Implementation
[0042] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, and not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0043] In this disclosure, the value of the read flag variable and the value of the data read position variable are used to determine whether other processes are reading data, thereby solving the data contention problem in multi-threaded reading in a single writer and multiple readers scenario.
[0044] Figure 1 A flowchart illustrating a single-writer, multi-reader data storage and retrieval method according to an embodiment of the present disclosure is shown.
[0045] It should be noted that during execution Figure 1 Before using the method shown, the data and variables used need to be initialized. Specifically, you can define an array A with a size of n+1, a read position variable R with a value of 0, a write position variable W with a value of 0, a read position atom variable aR with a value of 0, and a read flag variable t.
[0046] Then, based on the defined array and variable, taking reading I bytes of data as an example, the method in the following is executed. Figure 1
[0047] As shown in Figure 1 , the data storage and reading method of the single-writer multi-reader comprises:
[0048] S101: a reading thread acquires the value of a read position atomic variable and the value of a read position variable in an array;
[0049] S102: a read flag variable is set, and the value of the read flag variable is the value of the read position atomic variable;
[0050] S103: the value of the read position atomic variable is increased by a preset size of to-be-read data;
[0051] S104: if the value of the read flag variable is the same as the value of the data read position variable in the array, the to-be-read data is read from the array based on the position pointed by the data read position variable;
[0052] S105: if the value of the read flag variable is not the same as the value of the data read position variable in the array, other reader threads are reading, the to-be-read data is not read from the array, and the value of the read atomic position variable after the increase is reduced by the preset size of to-be-read data.
[0053] It can be understood that steps S104 and S105 are selectively executed according to the comparison result of the value of t and the value of R.
[0054] For the convenience of understanding, taking the case that all values are initial values as an example, two cases of reading with other readers and reading without other readers are analyzed as follows:
[0055] When no other reader is reading, in step S101, the value of the read position atomic variable aR and the value of the read position variable R are both 0;
[0056] In step S102, the value of the read flag variable t is set to the value of the read position atomic variable aR, that is, t=0;
[0057] In step S103, aR=aR+I, at this time, it can be seen that the value of aR has changed;
[0058] t=R=0, step S104 is executed, and reading is continued, that is, reading starts from the position of the read position variable R.
[0059] When other readers are reading, in step S101, R=0, since the value of aR is changed by other readers in step S103 of other readers, other readers read I其它 byte of data, so at this time aR = aR + I 其它 = 0 + I 其它 = I 其它 ;
[0060] In step S102, t = I 其它 ;
[0061] In step S103, aR = I 其它 + I;
[0062] Since t = I 其它 ≠ R, it is determined that there is another reader reading, so reading is stopped and aR = I 其它 + I - I = I 其它 , that is, aR is restored.
[0063] From the above analysis, it can be seen that as long as there is another reader reading data from the array, the value of the read position atomic variable will change, and the value of the read position atomic variable in the reading process has not changed, so it can be determined whether there is another reader reading according to the change of the value of the atomic variable, that is, comparing the value of the read flag variable t with the value of the read position variable. In the case where there is no other reading process, the data to be read is read, and in the case where there is another reading process, the reading is not performed, that is, S104 or S105 is executed, so as to realize the problem of data competition by lockless in the case where there are multiple reading processes.
[0064] It should be noted that the meaning of the read position variable R is to start reading from the data position, and the meaning of the read atomic position variable aR is to read the data position at the end of reading.
[0065] In steps S102 and S103, the process of changing the value of the read position atomic variable aR and assigning the value of the read flag variable t can be represented as t = atomic_add (aR, I) - I.
[0066] In step S104, based on the position pointed to by the data read position variable, the preset data to be read is read from the array, including:
[0067] Obtaining the value of the write position variable in the array;
[0068] Determining the remaining read space of the array according to the value of the read position variable and the value of the write position variable;
[0069] If the remaining read space is greater than or equal to the space occupied by the data to be read, the data to be read is read from the array based on the position pointed to by the data read position variable.
[0070] The remaining read space is denoted as REST.
[0071] According to an embodiment of the present disclosure, if the remaining read space is greater than or equal to the space occupied by the data to be read, it indicates that the remaining read space is sufficient to completely read the data to be read, and the data to be read is read from the array based on the position indicated by the data read position variable.
[0072] In some embodiments, determining the remaining read space of the array according to the value of the read position variable and the value of the write position variable comprises:
[0073] If the value of the write position variable is less than the value of the read position variable, the remaining read space is the sum of the capacity value of the array and the value of the write position variable minus the value of the read position variable.
[0074] If the value of the write position variable is greater than or equal to the value of the read position variable, the remaining read space is the value of the write position variable minus the value of the read position variable.
[0075] That is, if W < R, then REST = W + n + 1 - R, and if W ≥ R, then REST = W - R.
[0076] In some embodiments, in the case where the remaining read space is less than the space occupied by the data to be read, the data to be read is not read.
[0077] It can be understood that, in the case where the remaining read space is less than the space occupied by the data to be read, it indicates that the I-byte data to be read cannot be completely read, and then the terminal device does not read the data to be read, and needs to wait for the writer to write some data to increase the remaining read space in the array to achieve sufficient reading of the data to be read, and perform atomic_sub(aR, I) to return to S101.
[0078] In some embodiments, reading the preset data to be read from the array based on the position indicated by the data read position variable further comprises:
[0079] When the array reads to the tail element position and the data to be read has not completely read the array, the part of the data to be read that has not read the array is read from the head element position of the array until the data of the size of the data to be read is read.
[0080] In order to make the reading circulate, the array can be a circular array, and therefore, in the process of reading the preset data to be read from the array based on the position indicated by the data read position variable R, the data to be read can be sequentially read from the position indicated by the data read position variable R of the array, and if the reading reaches the tail of the array, the reading continues from the head of the array until the I-byte data is read.
[0081] The tmpr is calculated as R+I. If tmpr is less than n+1, it means that the data to be read cannot be read to the end of the array, the value of R is updated as tmpr. If tmpr is greater than or equal to n+1, it means that the data to be read can be read to the end of the array, the value of R is updated as tmpr-n-1, so as to represent the value of R in the size range of n+1 of the array.
[0082] In some embodiments, if the value of the read position variable plus the size of the data to be read is greater than the size of the array, the value of the read atomic position variable after the value is increased is subtracted by the size of the array.
[0083] That is, when tmpr is greater than or equal to n+1, atomic_sub(aR, n+1) is executed, so as to represent the value of aR after the change in the size range of n+1 of the array.
[0084] In order to more clearly describe the data storage process, in some embodiments, the processes of S101-S105 can also be shown in the read process diagram as shown in Figure 2 .
[0085] In some embodiments, the method shown in Figure 1 may further include a data writing process, Figure 3 a write process diagram according to an embodiment of the present disclosure is shown.
[0086] As shown in Figure 3 , the write thread includes:
[0087] The size of the writable data is determined according to the value of the read position variable and the value of the write position variable;
[0088] If the size of the writable data is greater than the size of the expected write data, the data stored in the array is written based on the position pointed by the write position variable, and the value of the write position variable is updated, wherein the size of the expected write data is the size of the data to be read minus the size of the remaining read space.
[0089] According to an embodiment of the present disclosure, in the data writing process, the write thread determines the size of the writable data REST according to the value of the read position variable R and the value of the write position variable W. If REST is greater than or equal to the size of the expected write data, the expected write data is written based on the position pointed by the write position variable, and the value of the write position variable R is updated, wherein the size A of the expected write data is the size of the data to be read minus the size of the remaining read space. If REST is less than the size A of the expected write data, no data is written, and the starting position is returned.
[0090] In some embodiments, the size of the writeable data is determined according to the value of the read position variable and the value of the write position variable, including:
[0091] If the value of the write position variable is less than the value of the read position variable, the size of the writeable data is determined according to the difference between the value of the read position variable and the value of the write position variable minus one;
[0092] If the value of the write position variable is greater than the value of the read position variable, the size of the writeable data is the sum of the capacity value of the array and the value of the read position variable minus the value of the write position variable minus one.
[0093] That is, if W < R, then REST = R - W - 1; if W ≥ R, then REST = n - W + R.
[0094] It should be further noted that, in some embodiments, when writing data into the array, the writing starts from the W position of the array, and if the writing reaches the end of the array, the writing continues from the head of the array. In the process of updating the value of the write position variable, tmpw = W + A is calculated, and if tmpw ≤ n, the value of the updated write position variable W is set as tmpw, otherwise the value of the updated write position variable W is W = tmpr - n - 1.
[0095] The present disclosure determines whether there is other process reading data according to the value of the read flag variable and the value of the data read position variable, to solve the data race problem among multiple reading processes in the scheme of solving shared memory without locking, thereby saving computing resources and improving resource utilization.
[0096] It should be noted that, for the foregoing method embodiments, in order to simply describe, they are all expressed as a series of action combinations, but those skilled in the art should know that the present disclosure is not limited by the order of the described actions, because according to the present disclosure, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification all belong to optional embodiments, and the actions and modules involved are not necessarily necessary for the present disclosure.
[0097] The above is the introduction of the method embodiment, and the following will further illustrate the scheme of the present disclosure through the device embodiment.
[0098] Figure 4 A block diagram of a single-writer multiple-reader data storage and reading device according to an embodiment of the present disclosure is shown. As shown in Figure 4 The device 400 includes:
[0099] 401: an acquisition module, configured to make a reading thread acquire a value of a read position atomic variable and a value of a read position variable in an array;
[0100] 402: an assignment module, configured to set a read flag variable, and the value of the read flag variable is the value of the read position atomic variable;
[0101] 403: a read position atomic variable adjustment module, configured to increase the value of the read position atomic variable by a preset size of to-be-read data;
[0102] 404: a processing module, configured to, if the value of the read flag variable is same as the value of the data read position variable in the array, read the to-be-read data from the array based on a position pointed by the data read position variable; if the value of the read flag variable is not same as the value of the data read position variable in the array, other reading thread is reading, and the to-be-read data is not read from the array, and the value of the read atomic position variable after the value is increased is subtracted by the preset size of to-be-read data.
[0103] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the described modules can refer to the corresponding process in the foregoing method embodiments, which will not be described here.
[0104] In the technical solution of the present disclosure, the acquisition, storage and application of user personal information comply with relevant laws and regulations and do not violate public order and good customs.
[0105] According to the embodiments of the present disclosure, the present disclosure further provides an electronic device, comprising: at least one processor; and a memory in communication connection with the at least one processor; characterized in that the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the above-mentioned method 100.
[0106] Figure 5 A schematic block diagram of an electronic device 500 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptops, desktops, tablets, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular telephones, smartphones, wearable devices, and other similar computing devices. The components shown here, their connections and relationships, and their functions, are meant to be examples only, and are not intended to limit the implementations of the present disclosure described and / or claimed in this document.
[0107] The device 500 includes a computing unit 501 that can perform various appropriate actions and processes in accordance with a computer program stored in a read only memory (ROM) 502 or a computer program loaded from a storage unit 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the device 500 can also be stored. The computing unit 501, the ROM 502, and the RAM 503 are connected to each other through a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0108] A plurality of components in the device 500 are connected to the I / O interface 505, including an input unit 506 such as a keyboard, a mouse, and the like, an output unit 507 such as various types of displays, speakers, and the like, a storage unit 508 such as a magnetic disk, an optical disk, and the like, and a communication unit 509 such as a network card, a modem, a wireless communication transceiver, and the like. The communication unit 509 allows the device 500 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0109] The computing unit 501 can be various general and / or special purpose processing components having processing and computing capabilities. Some examples of the computing unit 501 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, and the like. The computing unit 501 performs various methods and processes described above, such as the method 100. For example, in some embodiments, the method 100 can be implemented as a computer software program tangibly embodied in a machine-readable medium, such as the storage unit 508. In some embodiments, part or all of the computer program can be loaded and / or installed on the device 500 via the ROM 502 and / or the communication unit 509. When the computer program is loaded into the RAM 503 and executed by the computing unit 501, one or more steps of the method 100 described above can be performed. Alternatively, in other embodiments, the computing unit 501 can be configured to perform the method 100 by any other appropriate means, such as by means of firmware.
[0110] The various embodiments of the systems and techniques described above can be implemented in digital electronic circuitry, integrated circuitry, a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on a chip (SOC), a load programmable logic device (CPLD), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include implementation in one or more computer programs that are executable and / or interpretable on a programmable system including at least one programmable processor, which can be special or general purpose, coupled to receive data and instructions from, and to transmit data and instructions to, a storage system, at least one input device, and at least one output device.
[0111] Program code for carrying out methods of the present disclosure can be written in any combination of one or more programming languages. The program code can be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the program code, when executed by the processor or controller, produces the functions / operations specified in the flowcharts and / or the block diagrams. The program code can be executed entirely on a machine, partially on a machine, partially on a machine as a stand-alone software package, or entirely on a remote machine or server.
[0112] In the context of the present disclosure, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium will include one or more lines of electrical conductors, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0113] To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user and a keyboard and a pointing device (e.g., a mouse or a trackball) by which the user can provide input to the computer. Other kinds of devices can be used to provide for interaction with a user as well; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form, including acoustic, speech, or tactile input.
[0114] The systems and techniques described here can be implemented in a computing system that includes a back end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front end component (e.g., a user computer having a graphical user interface or a Web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back end, middleware, or front end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network (LAN), a wide area network (WAN), and the Internet.
[0115] The computer system can include clients and servers. A client and server are generally remote from each other and typically interact through a communication network. The relationship of client and server is generally established by computer programs running on the respective computers and having a client-server relationship to each other. The servers can be cloud servers, servers of a distributed system, or servers combined with a blockchain.
[0116] It should be understood that various forms of flow shown above can be used, with steps reordered, added, or removed. For example, the steps recited in the present disclosure can be performed in parallel, in series, or in a different order, without limitation, as long as the desired results of the technology disclosed in the present disclosure are achieved.
[0117] The specific embodiments described above are not intended to limit the scope of the present disclosure. Those skilled in the art will understand that various modifications, combinations, sub-combinations, and alternatives can be made to the specific embodiments without departing from the spirit and principles of the present disclosure. Any further modifications, equivalent substitutions, improvements, and the like, either alone or in some combination or sub-combination, are intended to be included within the scope of the present disclosure.
Claims
1. A method for data storage and retrieval by a single writer and multiple readers, characterized in that, include: The read thread retrieves the value of the atomic variable for the read position and the value of the read position variable in the array; Set a read flag variable, the value of which is the value of the read position atomic variable, wherein the read flag variable t = atomic_add(aR,I)-I, aR is the read position atomic variable, and I is the number of bytes of data read; Increase the value of the atomic variable at the read position by the preset size of the data to be read; If the value of the read flag variable is the same as the value of the data read position variable in the array, then the data to be read is read from the array based on the position pointed to by the data read position variable; If the value of the read flag variable is different from the value of the data read position variable in the array, then another reader thread is reading, and the data to be read is not read from the array. Instead, the value of the read atomic position variable after incrementing is subtracted from the preset size of the data to be read.
2. The data storage and retrieval method for a single writer and multiple readers as described in claim 1, characterized in that, Based on the position pointed to by the data read position variable, the preset data to be read is read from the array, including: Retrieve the value of the position variable written in the array; The remaining read space of the array is determined based on the values of the read position variable and the write position variable; If the remaining read space is greater than or equal to the space occupied by the data to be read, then the data to be read is read from the array based on the position pointed to by the data read position variable.
3. The data storage and retrieval method for a single writer and multiple readers as described in claim 2, characterized in that, Determining the remaining read space of the array based on the values of the read position variable and the write position variable includes: If the value of the write position variable is less than the value of the read position variable, then the remaining read space is the sum of the array capacity and the value of the write position variable minus the value of the read position variable; If the value of the write position variable is greater than or equal to the value of the read position variable, then the remaining read space is the value of the write position variable minus the value of the read position variable.
4. The data storage and retrieval method for a single writer and multiple readers as described in claim 2, characterized in that, If the remaining read space is less than the space occupied by the data to be read, the data to be read will not be read.
5. The data storage and retrieval method for a single writer and multiple readers as described in claim 2, characterized in that, Based on the position pointed to by the data reading position variable, reading preset data to be read from the array also includes: When the array is read to the last element position and the data to be read has not been completely read from the array, the part of the data to be read that has not been read from the array is read from the first element position of the array until the data to be read is read.
6. The data storage and retrieval method for a single writer and multiple readers as described in claim 5, characterized in that, If the value of the read position variable plus the size of the data to be read is greater than the array size, then the value of the read atomic position variable after the increment is subtracted from the array size.
7. The data storage and retrieval method for a single writer and multiple readers as described in claim 2, characterized in that, It also includes write threads, including: The size of the writable data is determined based on the values of the read position variable and the write position variable. If the writable data is greater than the expected writable data size, then based on the position pointed to by the write position variable, the data stored in the array is written, and the value of the write position variable is updated, wherein the expected writable data size is the size of the data to be read minus the size of the remaining read space.
8. The data storage and retrieval method for a single writer and multiple readers as described in claim 6, characterized in that, Determining the size of writable data based on the values of the read position variable and the write position variable includes: If the value of the write position variable is less than the value of the read position variable, then the size of the writable data is determined by subtracting one from the value of the write position variable from the value of the read position variable. If the value of the write position variable is greater than the value of the read position variable, then the size of the writable data is the sum of the array capacity and the value of the read position variable minus the value of the write position variable minus one.
9. A data storage and retrieval device with a single writer and multiple readers, characterized in that, include: The retrieval module enables the read thread to retrieve the values of the atomic variables at the read positions in the array, as well as the values of the read position variables. The assignment module is used to set the read flag variable, the value of which is the value of the read position atomic variable, wherein the read flag variable t = atomic_add(aR,I)-I, aR is the read position atomic variable, and I is the number of bytes of data read; The read position atomic variable adjustment module is used to increase the value of the read position atomic variable by a preset size of the data to be read; The processing module is configured to read the data to be read from the array based on the position pointed to by the data read position variable if the value of the read flag variable is the same as the value of the data read position variable in the array; if the value of the read flag variable is different from the value of the data read position variable in the array, and another reader thread is reading, the module will not read the data to be read from the array, and will subtract the preset size of the data to be read from the value of the read atomic position variable after the value is increased.
10. An electronic device, characterized in that, include: At least one processor; The at least one processor is also connected in communication with a memory; characterized in that the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-8.
Citation Information
Patent Citations
Multi-producer and single-consumer data storage and reading method, device and equipment
CN113377295A