Communication method, device, system, medium and product
By adopting lock-free message queueing technology in a multi-threaded environment, the performance bottleneck problem of traditional lock mechanism in high concurrency is solved, efficient data exchange and communication is achieved, deadlocks and delays are avoided, and programming process is simplified.
Patent Information
- Application Number
- CN202411272283.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-11
- Publication Date
- 2024-12-31
AI Technical Summary
In multi-threaded or multi-task environments, traditional locking mechanisms may introduce performance bottlenecks in high concurrency situations, resulting in competition and blockage between threads, making it difficult to achieve efficient data exchange and communication.
Using lock-free message queue technology, by using lock-free queue storage area and message queue control body in shared memory, lock-free write and read between multiple write parties is achieved, avoiding the use of locks, thereby improving the use of locks. Concurrency performance and system response speed.
It realizes efficient multi-threaded communication, reduces message delivery delay, avoids deadlock situations, simplifies programming complexity, and improves the efficiency and accuracy of data writing and reading.
Smart Images

Figure CN119225987A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of vehicle technology, and in particular to communication methods, devices, systems, media and products. Background Art
[0002] In a multi-threaded or multi-tasking environment, threads or tasks need to exchange and communicate data. Although the traditional lock mechanism can ensure thread safety, it may introduce performance bottlenecks in high concurrency situations because locks can cause competition and blocking between threads.
[0003] How to achieve multi-threaded communication through lock-free message queues has become an urgent problem to be solved. Summary of the invention
[0004] One of the purposes of the present application is to provide a communication method, device, system, medium and product, which can achieve lock-free writing when multiple writers write data, and has the characteristics of high security, simple and convenient implementation, high data writing efficiency and high accuracy.
[0005] In order to achieve the above purpose, the technical solution adopted in this application is as follows:
[0006] In the first aspect, the present application provides a communication method, which is applied to a first writer in a communication system, wherein the communication system includes N writers and one reader, N being an integer greater than 1; the first writer is any writer among the N writers; the method includes: determining a first message queue control body of the first writer among N message queue control bodies included in a shared memory of the communication system; one writer corresponds to one message queue control body; writing first data of the first writer to at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; updating the queue attributes of the first message queue control body; wherein, in the queue attributes of the updated first message queue control body, the status of each first data storage block in at least one first data storage block is a use status.
[0007] It can be seen that in this implementation, the data writing of the writer is realized through the lock-free queue storage area. And the entire implementation process is realized through the message queue control body. For one writer, one message queue control body corresponds to one writer. Multiple writers can write to their corresponding message queue control bodies respectively. The writing process of multiple writers is not affected, and lock-free writing is realized.
[0008] In this way, firstly, since the lock-free message queue does not need to use locks to implement concurrent access control, it is suitable for scenarios where push and pop are very frequent, and its efficiency is much higher than mutex; for example, stock quotes have at least hundreds of thousands of data in 1 second, and it has the characteristics of high concurrent performance and high data writing efficiency; secondly, since the lock-free message queue does not need to wait for the release of the lock, it can reduce the waiting time for communication between threads, thereby reducing the delay of message transmission, and has the characteristics of low latency; thirdly, the lock-free message queue will not have deadlock caused by lock competition because it does not use locks; the problem of deadlock can be avoided; thirdly, compared with using locks to implement concurrent control, the implementation of the lock-free message queue is relatively simple, which reduces the complexity of programming and has the characteristics of simple and convenient implementation; thirdly, the writing process of multiple writers does not affect each other, and it has the characteristics of high security and high accuracy.
[0009] In a possible implementation, a queue message control body includes: a queue storage area, queue attributes, and queue control information; the queue storage area includes multiple data storage blocks; the queue attributes include one or more of the following: the status of each data storage block in the multiple data storage blocks, the address of each data storage block, and the data length in each data storage block; the queue control information includes one or more of the following: a position pointer of the queue storage area, the number of written data, the number of read data, the identifier of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and a position pointer array of the queue storage area.
[0010] It can be seen that the queue message control body is used to manage the writing of a writer. In this embodiment, the content of the queue message control body is described in detail. First, the storage area, attribute area and management area are distinguished to achieve clear logic; secondly, the queue control information includes one or more items of the queue storage area location pointer, the number of written data, the number of read data, the identification of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and the queue storage area location pointer array, which can be configured according to actual conditions and has the characteristics of flexible implementation; thirdly, the queue storage area location pointer, the number of written data, the number of read data, the identification of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and the queue storage area location pointer array manage the control process in detail, improving the efficiency and accuracy of data writing.
[0011] In a possible implementation, when the queue control information in the first message queue control body includes the identifier of the writer, determining the message queue control body of the first writer among N message queue control bodies included in the shared memory of the communication system includes: searching for the identifier of the writer of each message queue control body in the multiple message queue control bodies; searching for the identifier of the first writer among the identifiers of the writer of each message queue control body, and determining the message queue control body to which the identifier of the first writer belongs as the first message queue control body.
[0012] It can be seen that in this implementation, the message queue control body is determined by writing the party identifier, which has the characteristics of being simple, fast, and not prone to errors.
[0013] In a possible implementation, before writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body, the method further includes: checking whether there is an idle data storage block in the queue storage area of the first message queue control body; performing a first processing if there is no idle data storage block in the queue storage area of the first message queue control body; until there is an idle data storage block in the queue storage area of the first message queue control body; if there is an idle data storage block in the queue storage area of the first message queue control body, determining the idle data storage block as at least one first data storage block.
[0014] It can be seen that in this implementation, before writing data, it is also necessary to determine whether there is a data storage block in an idle state, thereby improving the success rate of data writing and avoiding blocking.
[0015] In one possible implementation, when there is no data storage block in an idle state in the queue storage area of the first message queue control body, a first process is performed, including: when there is no data storage block in an idle state in the queue storage area of the first message queue control body, determining that the queue storage area of the first message queue control body is in a sending contention state; entering kernel state, and in kernel state, sending a first read instruction to a reader so that the reader reads data in the queue storage area of the first message queue control body; and returning to user state for processing.
[0016] It can be seen that in this implementation, the first processing is explained, and these processes are processed in the kernel state to improve security.
[0017] In a possible implementation, entering kernel state, in kernel state, sending a first read instruction to a reader so that the reader reads data from a queue storage area of a first message queue control body, and returning to user state for processing, including: in kernel state, sending a first read instruction to a reader so that the reader reads data from a queue storage area of a first message queue control body; if a data storage block in an idle state is obtained based on the instruction of the first read instruction, returning to user state to write the first data; if a data storage block in an idle state is not obtained based on the instruction of the first read instruction, returning to user state to wait;
[0018] Alternatively, in kernel mode, a first read instruction is sent to a reader within a first time so that the reader reads data from a queue storage area of a first message queue control body; if a data storage block in an idle state is obtained based on the indication of the first read instruction within the first time, the process returns to user mode to write the first data; or, if a data storage block in an idle state is not obtained based on the indication of the first read instruction after the first time is reached, the process returns to user mode to wait.
[0019] It can be seen that for the processing between kernel state and user state, this implementation method describes two processing methods, one with time limit and the other without implementation limit. It has the characteristics of flexible implementation, and the implementation method with time limit can avoid the situation of long waiting.
[0020] In a possible implementation, writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body includes: obtaining control information of the first message queue control body; determining whether the maximum supported data length in the control information meets the requirements of the first data; if the maximum supported data length does not meet the requirements of the first data, executing writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body; if the maximum supported data length does not meet the requirements of the first data, obtaining a newly applied first message queue control body, and writing the first data into at least one first data storage block in the queue storage area of the new first message queue control body; wherein the maximum supported data length in the control information in the new first message queue control body meets the requirements of the first data.
[0021] It can be seen that in this implementation, when writing the first data, it is also necessary to consider whether the maximum supported data length recorded in the control body of the message queue meets the requirements of the first data, that is, whether the maximum supported data length is greater than the data length of the first data. It can be written only if it is satisfied. If not, a new first message queue control body is re-applied for writing, thereby improving the success rate and accuracy of data writing.
[0022] In a possible implementation, the method further includes: upon completing the update of the queue attributes of the first message queue control body, sending a second read instruction to the reader, the second instruction read being used to instruct the reader to read the first data; or, in the event of a sending contention in the queue storage area in the first message queue control body, sending a first read instruction to the reader, the first read instruction being used to instruct the reader to read the data in the queue storage area.
[0023] It can be seen that in this implementation, after the writing of the first data is completed, the reader is notified that the data can be read, thereby improving efficiency; and when sending contention, the reader is notified to read the data to release space; the reader is reminded to read the data in time through the read instruction, which can improve efficiency and prevent blocking.
[0024] In a possible implementation, the queue storage area in the message queue control body is a ring queue storage area.
[0025] It can be seen that in this implementation, the circular queue storage area can realize cyclic writing and reading of data, and has the characteristics of high efficiency and simplicity.
[0026] In a second aspect, the present application provides a communication method, which is applied to a reader in a communication system, wherein the communication system includes N writers and one reader, where N is an integer greater than 1; the method includes: obtaining N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body; among the N message queue control bodies, determining a second message queue control body where the second data to be read is located; reading the second data in at least one second data storage block in the queue storage area of the second message queue control body; the queue storage area is a lock-free queue storage area; updating the queue attributes of the second message queue control body, wherein, in the queue attributes of the updated second message queue control body, the state of each second data storage block in at least one second data storage block is an idle state.
[0027] It can be seen that in this embodiment, the data reading of the reader is realized through the lock-free queue storage area, and the entire implementation process is realized through the message queue control body.
[0028] In this way, firstly, since the lock-free message queue does not need to use locks to implement concurrent access control, it is suitable for scenarios where push and pop are very frequent, and its efficiency is much higher than mutex; for example, stock quotes have at least hundreds of thousands of data in 1 second, and it has the characteristics of high concurrent performance and high data reading efficiency; secondly, since the lock-free message queue does not need to wait for the release of the lock, it can reduce the waiting time for communication between threads, thereby reducing the delay of message transmission, and has the characteristic of low latency; thirdly, the lock-free message queue will not have deadlock caused by lock competition because it does not use locks; the problem of deadlock can be avoided; thirdly, compared with using locks to implement concurrent control, the implementation of the lock-free message queue is relatively simple, which reduces the complexity of programming and has the characteristics of simple and convenient implementation.
[0029] In a possible implementation, among N message queue control bodies, determining the second message queue control body where the second data to be read is located includes: determining the writer to which the second data belongs; and among multiple message queue control bodies, determining the second message queue control body based on the identifier of the writer to which the second data belongs.
[0030] It can be seen that in this implementation, the message queue control body is determined by using the writing party identifier, which has the characteristics of being simple, fast, and not prone to errors.
[0031] In one possible implementation, the second data is read from at least one second data storage block in the queue storage area of the second message queue control body, including: checking whether there is a data storage block in use in the queue storage area of the second message queue control body; if there is no data storage block in use in the queue storage area of the second message queue control body, performing a second process until there is a data storage block in use in the queue storage area of the second message queue control body; if there is a data storage block in use in the queue storage area of the second message queue control body, determining that the data storage block in use is a second data storage block.
[0032] It can be seen that in this implementation, before reading the data, it is also necessary to determine whether there is a data storage block in use, that is, it is necessary to first determine whether the data is written, and then read it after writing, thereby improving the success rate of data reading and avoiding the blocking problem caused by waiting.
[0033] In one possible implementation, before executing the reading of the second data from at least one second data storage block in the queue storage area of the second message queue control body, the method also includes: when there is no data storage block in use in the queue storage area of the second message queue control body, determining that the queue storage area is in a receive race state; entering kernel state, and in kernel state, sending a first write instruction to the writer corresponding to the second message queue control body to write the second data, and returning to user state for processing.
[0034] It can be seen that in this implementation, the second processing is explained, and these processes are placed in the kernel state for processing, which improves security.
[0035] In a possible implementation, in kernel mode, sending a first write instruction to a writer corresponding to the second message queue control body to write the first data, and returning to user mode for processing, including: in kernel mode, sending a first write instruction to a writer corresponding to the second message queue control body to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction; returning to user mode to read the second data; if a data storage block in use is not obtained based on the instruction of the first instruction, returning to user mode to wait;
[0036] Alternatively, in kernel state, a first write instruction is sent to the writer corresponding to the second message queue control body within a second time to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction within the second time, then the process returns to user state to read the second data; or, after the second time is reached, if a data storage block in use is not obtained based on the instruction of the first instruction, then the process returns to user state to wait.
[0037] It can be seen that in this embodiment, for the processing between kernel state and user state, this embodiment describes two processing methods with time limit and without implementation limit. It has the characteristics of flexible implementation, and the implementation method with time limit can avoid the situation of long waiting.
[0038] In a possible implementation, the method further includes: upon completing the update of the queue attributes of the second message queue control body, sending a second write instruction to the reader corresponding to the second message queue control body, the second write instruction being used to instruct the reader corresponding to the second message queue control body to write new data; or, in the event of a receive contention occurring in the queue storage area in the second message queue control body, sending a first write instruction to the reader corresponding to the second message queue control body, the second instruction being used to instruct the reader corresponding to the second message queue control body to write the second data.
[0039] It can be seen that in this implementation, after completing the reading of the second data, the writer is notified that new data can be written, thereby improving efficiency; and when receiving contention, even if the writer is notified to write data, the writer is promptly reminded to write data through the write instruction, thereby improving efficiency and preventing blocking.
[0040] In a possible implementation, before obtaining the N message queue control bodies included in the shared memory of the communication system, the method also includes: determining the shared memory of the communication system; applying for N message queue control bodies in the shared memory; one writer corresponds to one message queue control body; and initializing the N message queue control bodies based on the needs of the N writers.
[0041] It can be seen that in this implementation, it is necessary to apply for shared memory, and apply for a corresponding number of message queue control bodies in the shared memory according to the number of writers, and perform initialization processing; it provides accuracy for subsequent lock-free communication and has the characteristics of simple implementation.
[0042] In a third aspect, the present application provides a first communication device. The first communication device is deployed at a first writing party in a communication system, the communication system includes N writing parties and one reading party, N is an integer greater than 1; the first writing party is any writing party among the N writing parties; the first communication device includes:
[0043] A first determining unit is used to determine a first message queue control body of a first writer among N message queue control bodies included in a shared memory of the communication system; one writer corresponds to one message queue control body;
[0044] A writing unit, used for writing first data of a first writing party into at least one first data storage block in a queue storage area of a first message queue control body; the queue storage area is a lock-free queue storage area;
[0045] The first updating unit is used to update the queue attribute of the first message queue control body; wherein, in the queue attribute of the updated first message queue control body, the state of each first data storage block in at least one first data storage block is in use state.
[0046] In a fourth aspect, the present application further provides a second communication device, which is deployed on a reader in a communication system, wherein the communication system includes N writers and one reader, where N is an integer greater than 1; the second communication device includes:
[0047] An acquisition unit, used for acquiring N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body;
[0048] A second determining unit, used to determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located;
[0049] A reading unit, used for reading second data from at least one second data storage block in a queue storage area of a second message queue control body; the queue storage area is a lock-free queue storage area;
[0050] The second updating unit is used to update the queue attribute of the second message queue control body, wherein in the queue attribute of the updated second message queue control body, the state of each second data storage block in the at least one second data storage block is an idle state.
[0051] In a fifth aspect, the present application further provides a communication system, the communication system comprising N writers and one reader, where N is an integer greater than 1;
[0052] Any one of the N writers is used to:
[0053] A first message queue control body of a first writer is determined in N message queue control bodies included in a shared memory of a communication system; one writer corresponds to one message queue control body; first data of the first writer is written into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; queue properties of the first message queue control body are updated; wherein, in the queue properties of the updated first message queue control body, the state of each first data storage block in at least one first data storage block is a use state; the first writer is any writer among the N writers;
[0054] The reader is used to:
[0055] Acquire N message queue control bodies included in the shared memory of the communication system; determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located; read the second data from at least one second data storage block in the queue storage area of the second message queue control body; update the queue attributes of the second message queue control body, wherein, in the queue attributes of the updated second message queue control body, the state of each second data storage block in at least one second data storage block is an idle state.
[0056] In some embodiments, multiple writers among the N writers are used to perform in parallel:
[0057] A first message queue control body of a first writer is determined among N message queue control bodies included in a shared memory of a communication system; one writer corresponds to one message queue control body; first data of the first writer is written into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; queue properties of the first message queue control body are updated; wherein, in the queue properties of the updated first message queue control body, the state of each first data storage block in at least one first data storage block is a use state; the first writer is any writer among the multiple writers.
[0058] In a sixth aspect, the present application further provides a storage medium storing a computer program or instruction, which, when executed by a processor, implements the communication method provided in the first or second aspect above.
[0059] In a seventh aspect, the present application further provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a processor, the communication method provided in the first or second aspect above is implemented.
[0060] It should be noted that the technical effects of the third to seventh aspects can refer to the detailed descriptions of the first and second aspects mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] Figure 1 An optional structural diagram of a communication system provided in an embodiment of the present application;
[0062] Figure 2 A schematic diagram of a first optional flow chart of a communication method for a first writer provided in an embodiment of the present application;
[0063] Figure 3 A second optional flow chart of the communication method of the first writing party provided in the embodiment of the present application;
[0064] Figure 4 A third optional flow chart of the communication method of the first writer provided in the embodiment of the present application;
[0065] Figure 5 A fourth optional flow chart of the communication method of the first writer provided in the embodiment of the present application;
[0066] Figure 6 A fifth optional flow chart of the communication method of the first writer provided in the embodiment of the present application;
[0067] Figure 7 A first optional flow chart of a communication method for a reader provided in an embodiment of the present application;
[0068] Figure 8 A second optional flow chart of the communication method of the reader provided in the embodiment of the present application;
[0069] Fig. 9 A third optional flow chart of the communication method of the reader provided in the embodiment of the present application;
[0070] Fig.10 A fourth optional flow chart of the communication method of the reader provided in the embodiment of the present application;
[0071] Fig.11 An optional flowchart of a single message sending process provided in an embodiment of the present application;
[0072] Fig.12 An optional structural diagram of a message queue control body provided in an embodiment of the present application;
[0073] Fig.13 An optional flowchart of the work that the kernel needs to do in cooperation with the user when the user state needs to send or receive data provided in the embodiment of the present application;
[0074] Fig.14 An optional structural diagram of the memory of the lock-free message queue provided in the embodiment of the present application;
[0075] Fig.15 An optional flowchart of the kernel space processing process provided in the embodiment of the present application;
[0076] Fig.16 An optional structural diagram of a first communication device provided in an embodiment of the present application;
[0077] Fig.17 An optional structural diagram of a second communication device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0078] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the specific technical solution of the application will be further described in detail below in conjunction with the drawings in the embodiments of the present application. The following embodiments are used to illustrate the present application, but are not used to limit the scope of the present application.
[0079] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0080] In the following description, the terms "first\second\third" are used only as examples to distinguish different objects, and do not represent a specific order for the objects, nor do they have a limitation on the order of precedence. It is understandable that "first\second\third" can be interchanged with a specific order or order of precedence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0081] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0082] Embodiments of the present application provide a control system, method, storage medium, and program product for a vehicle.
[0083] For ease of understanding, the communication system in this application is first described.
[0084] Exemplary, reference Figure 1 As shown, the communication system 10 may include N writers 101 and one reader 102, where N is an integer greater than 1.
[0085] The embodiment of the present application does not specifically limit the number N of writers, which can be configured according to actual needs.
[0086] The writer 101 is used to write data. The reader 102 is used to read data. Here, N writers 101 and one reader 102 share a shared memory, the N writers 101 write data to the shared memory, and the reader 102 reads the written data from the shared memory.
[0087] The writer 101 may include but is not limited to a writer thread or a writer process, etc. The reader 102 may include but is not limited to a reader thread or a reader process, etc.
[0088] The writer 101 may be referred to as a sender or a producer; the reader may be referred to as a receiver or a consumer.
[0089] Exemplarily, the N writers 101 and one reader 102 may be N sending threads and one receiving thread in a process.
[0090] The following describes various embodiments of the communication method, device, system, medium, and product provided in the embodiments of the present application.
[0091] In a first aspect, an embodiment of the present application provides a communication method, which is applied to a first writer in a communication system, where the first writer is any writer among N writers.
[0092] The following takes the first writer as an example to illustrate the data writing process of the writer. For other writers among the N writers, the data writing process is similar to that of the first writer, and the detailed description of the first writer can be referred to, which will not be repeated here.
[0093] refer to Figure 2 The method may include but is not limited to the following S201 to S203.
[0094] S201. A first writer determines a first message queue control body of the first writer from N message queue control bodies included in a shared memory of a communication system.
[0095] One writer corresponds to one message queue control body, wherein the message control body corresponding to the first writer is the first message control body.
[0096] The shared memory of the communication system is used for the writer in the communication system to write data and the reader to read data. The embodiment of the present application does not limit the size and storage location of the shared memory of the communication system, and can be configured according to actual needs.
[0097] The message queue control body is used to store and manage data. The embodiment of the present application does not limit the specific information type included in the message queue control body, and can be configured according to actual needs.
[0098] Here, the specific implementation method of the first writer determining the first message queue control body among the N message queue control bodies is not limited and can be configured according to actual needs. For example, it can be determined based on the identifier of the first writer. Of course, it can also be determined by indexing the corresponding address, which will not be listed here one by one.
[0099] S202: The first writer writes the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body.
[0100] The queue storage area is a lock-free queue storage area.
[0101] The first data is the data to be written by the first writer this time. The size, content and quantity of the first data are not limited here and can be configured according to actual needs.
[0102] It should be noted that the number and size of the data written by the first writer multiple times may be the same or different. For example, the first writer may write 5 data for the first time and 2 data for the second time. The first data here may be one or more data.
[0103] The message queue control body includes a queue storage area, which is used to store data. Specifically, the queue storage area can be divided into multiple data storage blocks, and one data storage block is used to store one data (also called one message).
[0104] The first data storage block is a data storage block for storing the first data. The embodiment of the present application does not limit the method for determining the first data storage block, and it can be determined according to actual needs. For example, a data storage block in an idle state, that is, an unused state, can be searched in the queue storage area as the first data storage block.
[0105] S202 may be implemented as follows: the first writer searches for a queue storage area in the first message queue control body, determines at least one first data storage block in the queue storage area, and stores the first data in the at least one first data storage block.
[0106] S203: The first writer updates queue attributes of the first message queue control body.
[0107] Among them, in the queue attributes of the updated first message queue control body, the state of each first data storage block in the at least one first data storage block is in use state.
[0108] It is understandable that the update of the queue attribute may also include the update of other information, which will not be listed here one by one, such as the address of the first data storage block, that is, the storage address of the first data, the data size in each first data storage block, etc.
[0109] It can be seen that in the embodiment, the data writing of the writer is realized through the lock-free queue storage area. And the whole implementation process is realized through the message queue control body. For one writer, one message queue control body corresponds to one writer. Multiple writers can write to their corresponding message queue control bodies respectively. The writing process of multiple writers is not affected, and lock-free writing is realized.
[0110] In this way, firstly, since the lock-free message queue does not need to use locks to implement concurrent access control, it is suitable for scenarios where push and pop are very frequent, and its efficiency is much higher than mutex; for example, stock quotes have at least hundreds of thousands of data in 1 second, and it has the characteristics of high concurrent performance and high data writing efficiency; secondly, since the lock-free message queue does not need to wait for the release of the lock, it can reduce the waiting time for communication between threads, thereby reducing the delay of message transmission, and has the characteristics of low latency; thirdly, the lock-free message queue will not have deadlock caused by lock competition because it does not use locks; the problem of deadlock can be avoided; thirdly, compared with using locks to implement concurrent control, the implementation of the lock-free message queue is relatively simple, which reduces the complexity of programming and has the characteristics of simple and convenient implementation; thirdly, the writing process of multiple writers does not affect each other, and it has the characteristics of high security and high accuracy.
[0111] Next, the message queue control body is explained.
[0112] A queue message control body includes: queue storage area, queue attributes and queue control information.
[0113] The queue storage area includes a plurality of data storage blocks.
[0114] The queue attributes include one or more of the following: a state of each data storage block in the plurality of data storage blocks, an address of each data storage block, and a length of data in each data storage block.
[0115] The queue control information includes one or more of the following: a location pointer of the queue storage area, the number of written data, the number of read data, the identification of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and a location pointer array of the queue storage area.
[0116] in:
[0117] The queue storage area includes multiple data storage blocks, and the size of each data storage block can be the same or different, which is configured according to actual needs. The number of data storage blocks can also be configured based on the needs of the writer.
[0118] The state of each data storage block may include: an idle state and a used state. The idle state refers to a state where no data is written or a state where data has been read after being written; the used state refers to a state where data has not been read after being written.
[0119] The location pointer of the queue storage area includes the head pointer and the tail pointer.
[0120] The number of written data refers to the number of data storage blocks in the queue storage area where data can be written. For example, the number of written data may be the number of data storage blocks in an idle state.
[0121] The number of read data refers to the number of data storage blocks in the queue storage area from which data can be read. For example, the number of read data may be the number of data storage blocks in use.
[0122] The writer's identifier is used to uniquely point to a writer. The embodiment of the present application does not limit the specific implementation of the writer's identifier, and can be configured according to actual needs. For example, the writer's identifier can be a combination of letters, numbers, etc.
[0123] The maximum supported data length refers to the maximum length of data that can be stored in a data storage block. It can be configured according to the needs of the writer.
[0124] The number of data storage blocks included in the queue storage area can be configured according to the actual situation of the writer.
[0125] The position pointer array of the queue storage area includes a pointer array of each position in the queue storage area, and can point to the address of each data storage block in the queue storage area.
[0126] It can be seen that the queue message control body is used to manage the writing of a writer. In this embodiment, the content of the queue message control body is described in detail. First, the storage area, attribute area and management area are distinguished to achieve clear logic; secondly, the queue control information includes one or more items of the queue storage area location pointer, the number of written data, the number of read data, the identification of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and the queue storage area location pointer array, which can be configured according to actual conditions and has the characteristics of flexible implementation; thirdly, the queue storage area location pointer, the number of written data, the number of read data, the identification of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and the queue storage area location pointer array manage the control process in detail, improving the efficiency and accuracy of data writing.
[0127] Next, the process of determining the first message queue control body of the first writer from the N message queue control bodies included in the shared memory of the communication system in S201 is described.
[0128] In some embodiments, when the queue control information in the first message queue control body includes the identifier of the writer, refer to Figure 3 As shown in the content, the process may include but is not limited to the following S2011 and S2012.
[0129] S2011. The first writer searches for an identifier of a writer of each message queue control body in a plurality of message queue control bodies.
[0130] In a possible implementation, the position of the writer's identifier in the message queue control body is preset, and the first writer first locates the position of the writer's identifier and then reads the value of the position to obtain the writer's identifier of each message queue control body.
[0131] In another possible implementation, the first writer is defined as the control information area of each message control body, and then the writer identification field is located in the control information area, and then the content of the writer identification field is read to obtain the identification of the writer of each message queue control body.
[0132] S2012: The first writer searches for the identifier of the first writer in the identifiers of the writers of each message queue control body, and determines the message queue control body to which the identifier of the first writer belongs as the first message queue control body.
[0133] It can be seen that in this embodiment, the message queue control body is determined by writing the party identifier, which has the characteristics of being simple, fast and not prone to errors.
[0134] In some embodiments, reference Figure 4 The content shown may also include but is not limited to the following S204 to S206 before executing the above S202 where the first writer writes the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body.
[0135] S204: The first writer checks whether there is an idle data storage block in the queue storage area of the first message queue control body.
[0136] In a possible implementation, the first writer checks the queue attributes in the first message queue control body, checks the status of each data storage block in the queue attributes, and determines whether there is an idle data storage block in the queue storage area according to the storage status of each data block.
[0137] In another possible implementation, the first writer may check the control information in the first message queue control body, read the positions of the head pointer and the tail pointer in the control information, and if the positions of the head pointer and the tail pointer coincide, it is determined that there is no idle data storage block in the queue storage area; if the positions of the head pointer and the tail pointer do not coincide, it is determined that there is an idle data storage block in the queue storage area.
[0138] S205: When there is no idle data storage block in the queue storage area of the first message queue control body, the first writer performs a first process until there is an idle data storage block in the queue storage area of the first message queue control body.
[0139] In a possible implementation, the first process is waiting.
[0140] In another possible implementation, the first process is to notify the reader to read and then wait.
[0141] Other implementation methods are not listed one by one.
[0142] S206: When there is an idle data storage block in the queue storage area of the first message queue control body, the first writer determines the idle data storage block as at least one first data storage block.
[0143] It should be noted that if the number of idle data storage blocks is less than the number of data (messages) in the first data, part of the data will be written into these idle first data storage blocks first, and the other data will be written after the space is released.
[0144] It can be seen that in this implementation, before writing data, it is also necessary to determine whether there is a data storage block in an idle state, thereby improving the success rate of data writing and avoiding blocking.
[0145] Next, the process of the first writing party performing the first processing when there is no idle data storage block in the queue storage area of the first message queue control body in S205 is described.
[0146] In some embodiments, reference Figure 5 As shown in the content, the process may include but is not limited to the following S2051 and S2052.
[0147] S2051: When there is no idle data storage block in the queue storage area of the first message queue control body, determine that the queue storage area of the first message queue control body is in a sending contention state.
[0148] The sending race condition means that there is no idle data storage block in the queue storage area of the first message queue control body, that is, data cannot be written (sent).
[0149] S2052, entering the kernel state, and in the kernel state, sending a first read instruction to the reader so that the reader reads the data in the queue storage area of the first message queue control body; and returning to the user state for processing.
[0150] The first read instruction is used to instruct the reader to read the data in the queue storage area of the first message queue control body. There is no limitation on which data to read, and it can be determined according to actual needs.
[0151] It can be seen that in this embodiment, the first processing is explained, and these processes are processed in the kernel state to improve security.
[0152] Next, the process of entering the kernel state in S2052, sending a first read instruction to the reader in the kernel state so that the reader reads the data in the queue storage area of the first message queue control body, and returning to the user state for processing is described.
[0153] In some embodiments, the process may include but is not limited to the following method 1 or method 2.
[0154] In method 1, in kernel state, a first read instruction is sent to a reader so that the reader reads data in a queue storage area of a first message queue control body; if a data storage block in an idle state is obtained based on the instruction of the first read instruction, the method returns to user state to write the first data; if a data storage block in an idle state is not obtained based on the instruction of the first read instruction, the method returns to user state to wait.
[0155] In method 2, in kernel state, a first read instruction is sent to the reader within the first time so that the reader reads the data in the queue storage area of the first message queue control body; if a data storage block in an idle state is obtained based on the indication of the first read instruction within the first time, the method returns to the user state to write the first data; or, after the first time is reached, if a data storage block in an idle state is not obtained based on the indication of the first read instruction, the method returns to the user state to wait.
[0156] It can be seen that for the processing between kernel state and user state, this implementation method describes two processing methods, one with time limit and the other without implementation limit. It has the characteristics of flexible implementation, and the implementation method with time limit can avoid the situation of long waiting.
[0157] Next, the process in S202 where the first writer writes the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body is described.
[0158] In some embodiments, reference Figure 6 As shown in the content, the process may include but is not limited to the following S601 to S604.
[0159] S601: Obtain control information of a first message queue control body.
[0160] The first writer locates the control information field, reads the information in the field, and obtains the control information of the first message queue control body.
[0161] S602: Determine whether the maximum supported data length in the control information meets the requirement of the first data.
[0162] The maximum supported data length field is located in the control information, the pointer of the field is read, the value of the maximum supported data length is obtained, and the size between the value of the maximum supported data length and the data length value of the first data is determined. If the value of the maximum supported data length is greater than or equal to the data length value of the first data, it is determined that the requirement is met; if the value of the maximum supported data length is less than the data length value of the first data, it is determined that the requirement is not met.
[0163] S603: When the maximum supported data length does not meet the requirement of the first data, write the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body.
[0164] S604: When the maximum supported data length does not meet the requirements of the first data, obtain a newly applied first message queue control body, and write the first data into at least one first data storage block in the queue storage area of the new first message queue control body.
[0165] Among them, the maximum supported data length in the control information in the new first message queue control body meets the requirements of the first data.
[0166] If the requirements are not met, a new first message queue control body needs to be re-applied. When applying here, the maximum supported data length value in the control information of the new first message control body can be filled based on the data length value of the first data. In practice, the re-application of a new first message queue control body can be applied by the reader and then obtained and used by the first writer, or it can be directly applied by the first writer.
[0167] It can be seen that in this embodiment, when writing the first data, it is also necessary to consider whether the maximum supported data length recorded in the control body of the message queue meets the requirements of the first data, that is, whether the maximum supported data length is greater than the data length of the first data. It can be written only when it is satisfied. If not, a new first message queue control body is re-applied for writing, which improves the success rate and accuracy of data writing.
[0168] It should be noted that before writing the first data, a process of determining whether there is an idle data storage block in the queue storage area of the first message queue control body can be performed, and a process of determining whether the maximum supported data length meets the requirements of the first data can also be performed. Only when both conditions are met, the writing of the first data is executed. For the specific implementation, reference can be made to the detailed description of the two processes, which will not be repeated here one by one.
[0169] In some embodiments, the method further includes a process of sending a read instruction, which may specifically include but is not limited to the following two situations.
[0170] Case 1: After completing the update of the queue attributes of the first message queue control body, a second read instruction is sent to the reader.
[0171] The second instruction read is used to instruct the reader to read the first data.
[0172] Case 2: When a sending race occurs in the queue storage area in the first message queue control body, a first reading instruction is sent to the reader.
[0173] The first read instruction is used to instruct the reader to read the data in the queue storage area.
[0174] In simple terms, the first read instruction is a general read instruction, which does not limit which data is to be read. The second read instruction is an instruction indicating that the first data can be read, and refers to a read instruction for the first data.
[0175] It can be seen that in this implementation, after the writing of the first data is completed, the reader is notified that the data can be read, thereby improving efficiency; and when sending contention, the reader is notified to read the data to release space; the reader is reminded to read the data in time through the read instruction, which can improve efficiency and prevent blocking.
[0176] In a possible implementation, the queue storage area in the message queue control body is a circular queue storage area. Of course, the queue storage area in the message queue control body can also be a non-circular queue storage area. When the queue storage area is a circular queue storage area, the circular queue storage area can realize the cyclic writing and reading of data, which has the characteristics of high efficiency and simplicity.
[0177] In a second aspect, an embodiment of the present application provides a communication method, which is applied to a reader in a communication system.
[0178] refer to Figure 7 The method may include but is not limited to the following S701 to S704.
[0179] S701: A reader obtains N message queue control bodies included in a shared memory of a communication system.
[0180] One writer corresponds to one message queue control body.
[0181] S702: The reader determines, from among the N message queue control bodies, a second message queue control body where the second data to be read is located.
[0182] The second data is the data to be read. The number of data, data size, data content, etc. included in the second data are not limited here and can be configured according to actual needs.
[0183] There is no limitation on the method of determining the second message queue control body here, and it can be configured according to actual needs.
[0184] For example, the writer corresponding to the second data is known, and the second message queue control body is determined according to the identifier of the writer corresponding to the second data.
[0185] For another example, the writer corresponding to the second data is known, and the second message queue control body is determined according to the index and address of the writer corresponding to the second data.
[0186] S703: The reader reads second data from at least one second data storage block in the queue storage area of the second message queue control body.
[0187] The queue storage area is a lock-free queue storage area.
[0188] The second data storage block refers to a data storage block in the queue storage area of the second message queue control body for storing the second data.
[0189] S704: The reader updates the queue attributes of the second message queue control body.
[0190] Among them, in the queue attributes of the updated second message queue control body, the state of each second data storage block in the at least one second data storage block is an idle state.
[0191] It can be understood that the update of queue attributes may also include the update of other information, which will not be listed here one by one.
[0192] It can be seen that in this embodiment, the data reading of the reader is realized through the lock-free queue storage area, and the entire implementation process is realized through the message queue control body.
[0193] In this way, firstly, since the lock-free message queue does not need to use locks to implement concurrent access control, it is suitable for scenarios where push and pop are very frequent, and its efficiency is much higher than mutex; for example, stock quotes have at least hundreds of thousands of data in 1 second, and it has the characteristics of high concurrent performance and high data reading efficiency; secondly, since the lock-free message queue does not need to wait for the release of the lock, it can reduce the waiting time for communication between threads, thereby reducing the delay of message transmission, and has the characteristic of low latency; thirdly, the lock-free message queue will not have deadlock caused by lock competition because it does not use locks; the problem of deadlock can be avoided; thirdly, compared with using locks to implement concurrent control, the implementation of the lock-free message queue is relatively simple, which reduces the complexity of programming and has the characteristics of simple and convenient implementation.
[0194] Next, the process of determining, by the reader in S702 , the second message queue control body where the second data to be read is located among the N message queue control bodies will be described.
[0195] In some embodiments, the process may include: the reader determines the writer to which the second data belongs; and among multiple message queue control bodies, determines the second message queue control body based on the identifier of the writer to which the second data belongs.
[0196] It can be seen that in this implementation, the message queue control body is determined by using the writing party identifier, which has the characteristics of being simple, fast, and not prone to errors.
[0197] In some embodiments, reference Figure 8 As shown in the content, before the reader reads the second data in at least one second data storage block in the queue storage area of the second message queue control body in S703, it may also include but not be limited to the following S705 to S707.
[0198] S705: The reader checks whether there is a data storage block in use in the queue storage area of the second message queue control body.
[0199] In a possible implementation, the reader checks the queue attributes in the second message queue control body, checks the status of each data storage block in the queue attributes, and determines whether there is a data storage block in use in the queue storage area according to the storage status of each data block.
[0200] In another possible implementation, the reader can check the control information in the second message queue control body, read the position of the head pointer and the tail pointer in the control information, and determine that there is a data storage block in use in the queue storage area based on the position between the head pointer and the tail pointer.
[0201] S706: When there is no data storage block in use in the queue storage area of the second message queue control body, the reader performs the second process until there is a data storage block in use in the queue storage area of the second message queue control body.
[0202] In a possible implementation manner, the second process is waiting.
[0203] In another possible implementation, the second process is to notify the writer to write and then wait.
[0204] Other implementation methods are not listed one by one.
[0205] S707: When there is a data storage block in use in the queue storage area of the second message queue control body, the reader determines that the data storage block in use is the second data storage block.
[0206] It can be seen that in this implementation, before reading the data, it is also necessary to determine whether there is a data storage block in use, that is, it is necessary to first determine whether the data is written, and then read it after writing, thereby improving the success rate of data reading and avoiding the blocking problem caused by waiting.
[0207] Next, the process of the reader performing the second processing when there is no data storage block in use in the queue storage area of the second message queue control body in S706 is described.
[0208] In some embodiments, reference Fig. 9 As shown in the content, the process may include but is not limited to the following S7061 and S7062.
[0209] S7061: When there is no data storage block in use in the queue storage area of the second message queue control body, determine that the queue storage area is in a receiving contention state.
[0210] The receiving race condition means that there are no idle data storage blocks in the queue storage area of the second message queue control body, that is, data cannot be read (received).
[0211] S7062, entering the kernel state, in the kernel state, sending a first write instruction to the writing party corresponding to the second message queue control body to write the second data, and returning to the user state for processing.
[0212] The first write instruction is used to instruct the writer to write second data into the queue storage area of the second message queue control body.
[0213] It can be seen that in this implementation, the second processing is explained, and these processes are placed in the kernel state for processing, which improves security.
[0214] The following describes the process of entering the kernel state in S7062, sending a first write instruction to the writer corresponding to the second message queue control body in the kernel state to write the second data, and returning to the user state for processing.
[0215] In some embodiments, the process may include but is not limited to the following method 3 or method 4.
[0216] In method 3: in kernel state, a first write instruction is sent to the writer corresponding to the second message queue control body to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction, the method returns to user state to read the second data; if a data storage block in use is not obtained based on the instruction of the first instruction, the method returns to user state to wait.
[0217] In method 4, in kernel state, a first write instruction is sent to the writer corresponding to the second message queue control body within the second time to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction within the second time, then the method returns to user state to read the second data; or, after the second time is reached, if a data storage block in use is not obtained based on the instruction of the first instruction, then the method returns to user state to wait.
[0218] It can be seen that in this embodiment, for the processing between kernel state and user state, this embodiment describes two processing methods with time limit and without implementation limit. It has the characteristics of flexible implementation, and the implementation method with time limit can avoid the situation of long waiting.
[0219] In some embodiments, the method further includes a process of sending a write instruction, which may specifically include but is not limited to the following two situations.
[0220] Case 3: After the queue attribute of the second message queue control body is updated, a second write instruction is sent to the reader corresponding to the second message queue control body.
[0221] The second write instruction is used to instruct the reader corresponding to the second message queue control body to write new data.
[0222] Case 4: When a receiving race occurs in the queue storage area in the second message queue control body, a first write instruction is sent to the reader corresponding to the second message queue control body.
[0223] The second instruction is used to instruct the reader corresponding to the second message queue control body to write the second data.
[0224] It can be seen that in this implementation, after completing the reading of the second data, the writer is notified that new data can be written, thereby improving efficiency; and when receiving contention, even if the writer is notified to write data, the writer is promptly reminded to write data through the write instruction, thereby improving efficiency and preventing blocking.
[0225] In some embodiments, reference Fig.10 As shown in the content, before executing S701 and the reader obtains N message queue control bodies included in the shared memory of the communication system, the method may also include but is not limited to the following S708 to S710.
[0226] S708: The reader determines the shared memory of the communication system.
[0227] A storage area is requested in the storage area of the reader as the shared memory of the communication system. The size of the shared memory and the location of the storage area can be configured based on actual conditions.
[0228] S709: The reader applies for N message queue control bodies in the shared memory.
[0229] One writer corresponds to one message queue control body.
[0230] The reader knows in advance that the number of writers is N, and then applies for N message queue control bodies in the shared memory.
[0231] S710: Based on the requirements of N writers, N message queue control bodies are initialized.
[0232] The initialization process here includes: filling the values or initial values of each item in the control information and the initial values of each item in the attribute information in the corresponding message queue control bodies based on the requirements of the N writers.
[0233] It can be seen that in this embodiment, it is necessary to apply for shared memory, and apply for a corresponding number of message queue control bodies in the shared memory according to the number of writers, and perform initialization processing; it provides accuracy for subsequent lock-free communication and has the characteristics of simple implementation.
[0234] Below, taking multiple sending threads and one receiving thread in a process as an example, the communication method provided in this embodiment of the present application is described.
[0235] In a multi-threaded or multi-tasking environment, threads or tasks need to exchange and communicate data. Although the traditional lock mechanism can ensure thread safety, it may introduce performance bottlenecks in high concurrency situations because locks can cause competition and blocking between threads.
[0236] The message queue, one of the communication mechanisms of Unix, is a container for storing messages. The Posix message queue implementation mechanism mainly relies on mutex locks and condition variables. Multithreaded communication within a process is usually carried out using memory shared between threads. Because in a multithreaded environment, threads share the address space of the same process, so they can directly access the same memory area. Multi-process communication generally uses communication methods such as pipes and sockets. Now there is also a shm communication method based on the memory file system.
[0237] The lock-free message queue avoids the use of locks by using special data structures and algorithms, thereby improving concurrency performance and system throughput. It usually uses some atomic operations, lock-free queues and other technologies to achieve safe data transmission and sharing, thereby avoiding competition and blocking for locks, and improving the system's concurrency and response speed. This technology is particularly useful in high-concurrency systems, such as servers, network communications, etc., which can effectively reduce system load and improve performance.
[0238] Related technology 1 provides a lock-free message queue communication method and system, which mainly includes: creating a lock-free message queue in a shared memory, the lock-free message queue includes at least one message body arranged according to the first-in-first-out principle; receiving a delivery message sent by at least one writing process, and writing the message body corresponding to the delivery message of each writing process to the tail of the queue of the lock-free message queue in sequence according to the order in which the delivery messages are received; receiving a read message sent by at least one reading process, and sending the message body to each reading process in sequence from the head of the queue of the lock-free message queue. In the lock-free message queue communication method and system, all message bodies are stored in the shared memory according to the first-in-first-out principle, and concurrent secure communication for large-scale data interaction between multiple processes in the same device can be realized through the writing process and the reading process.
[0239] The difference between the present invention and related technology 1 is that when related technology 1 receives and sends messages, the buffer storing data is not a circular buffer; the sending and receiving process requires strict queuing; the data is obtained according to the first-in-first-out principle; it supports multi-process communication and requires a message notification mechanism, and the notification link is relatively longer. The present invention is multi-threaded communication within the process, using a circular buffer; no queuing is required, and multiple parties can write in parallel; the data is sent and received using the ID method, and data is sent and received based on the ID; the idea of sending and receiving data is different.
[0240] Related technology 2 provides a method for implementing a lock-free message queue, which mainly includes: the main thread obtains a message and saves the message in a message queue; the main thread detects whether there is an idle worker thread among all worker threads; if not, it continues to detect all worker threads until an idle worker thread is detected, and then the main thread sends the message to the idle worker thread, the idle worker thread receives and processes the message, and the idle worker thread becomes a worker thread; after the worker thread processes the message, it sends an idle signal to the main thread. This patent provides a method for implementing a lock-free message queue, which improves performance, reduces the risk of program deadlock, and simplifies code debugging while avoiding locks.
[0241] The difference between the present invention and related technology 2: Related technology 2 is that the main thread determines whether to send or receive data based on whether other threads are busy. If other threads are not busy, the main thread will hand over the data to other threads for sending and receiving. The main thread of the present invention does not hand over the data sending and receiving work to other threads to complete. Instead, any thread within the process can send data, and only one thread needs to be designated to receive data. The idea of sending and receiving data is different.
[0242] The difference between the present invention and the open source lock-free message queue is: the open source lock-free message queue has multiple producers and multiple consumers, and the message queue is divided into two types, one is an explicit producer queue, and the other is an implicit producer queue. All explicit queues are managed through linked lists, and implicit queues are independent memory blocks. When the open source message queue data is dequeued, it is necessary to traverse the queue to find the data. In the worst case, the entire message queue is traversed. In actual use, the queue is shared and accessed by multiple threads, and the source code is written in C++, which is more complicated to implement. The open source implementation also uses semaphores, signals and other methods, which are not used in the present invention. In addition, the memory area of the lock-free message queue of the present invention is uniformly managed by consumers, and different message queues are distinguished by methods such as user thread ID.
[0243] The present invention mainly focuses on the lock-free high-speed communication between multiple threads within a process and one thread, and is applicable to the situation where there is one consumer (equivalent to the receiver, or the above-mentioned reader) and multiple producers (equivalent to the sender, or the above-mentioned writer). If it is communication between processes or multiple sends and multiple receivers of multiple threads within a process, the present invention is not applicable. For example, for the stock trading data scenario, the stock data generally comes from stock exchanges, securities companies, etc., and the collected data is transmitted to the data processing center or data analysis system through the network. The data center performs data analysis and calculation on the stock data to extract useful information and indicators for formulating investment strategies, risk management, decision support, etc. Then the collected data is multiple producers, and the analyzed and processed data is the consumer.
[0244] The following is an explanation of the process of sending a single message. Fig.11 As shown in the content, a single message sending process may include but is not limited to the following S1101 to S1110.
[0245] S1101. Apply for message control body.
[0246] S1102: Fill in the control body content.
[0247] S1103. Record the message control body and set the ID.
[0248] S1104, initialize the message sending and receiving linked list.
[0249] S1105. The sending thread obtains the message control body.
[0250] S1106. Fill in the message and send it.
[0251] S1107, fill in the linked list to detect the status.
[0252] S1108. Wake up the receiving thread.
[0253] S1109. Send the message location to the recipient.
[0254] S1110. Receive a message.
[0255] Figure 11 also illustrates the interaction process between the user space and the kernel when a single message is sent. Among them, S1101, S1102, S1105, S1106, and S1110 are implemented by user programs in user mode; S1103, S1104, S1107 to S1109 are implemented in kernel mode.
[0256] The process of sending and receiving data is described below, which may include but is not limited to the following steps 1 to 4.
[0257] Step 1: Apply for a message queue controller.
[0258] Apply for N message queue control blocks according to the needs. The message queue control block is presented in the form of a pointer array. Each message queue control block controls a message queue. The content is subdivided into the position of the sent and received data in the queue (send and receive position list), the ID of the message queue control block, the maximum message size (maximum data length), etc.
[0259] Among them, the message queue control body can refer to Fig.12 The contents shown include a transmit and receive position list 1201, a maximum data length 1202, a buffer length 1203, a number of received data 1204, a number of sent data 1205, and a queue length 1206.
[0260] In the sending and receiving position linked list 1201, the head and tail are used to control and query the position of the message in the message queue.
[0261] The maximum data length is 1202, which is the maximum value of the data sent by the user.
[0262] The buffer length 1203 is the size of a single storage area (a buffer) in the message queue.
[0263] The number of received data 1204 and the number of sent data 1205 are used to indicate how much data to be processed is in the message control body.
[0264] The queue length 1206 is the number of messages included in the message control queue. The number is based on actual demand and the maximum number cannot exceed 65534.
[0265] Of course, the message queue control body may also include other contents, which are not listed here one by one, such as thread ID (queue ID) and the like.
[0266] Step 2: Fill the message queue control body.
[0267] Because it is a message queue, it must be a collection of multiple messages. Therefore, it is necessary to create the maximum number of messages to be sent and received (queue length, that is, the number of messages in a queue) and the maximum message size to be sent (maximum data length) according to user needs, and manage the position of the message in the queue with a linked list.
[0268] It should be noted that not every message queue control body needs to be filled. In fact, as many queue control bodies as are needed in multi-threaded communication should be filled.
[0269] Step 3: Send data (equivalent to writing the above data).
[0270] Step 3.1, obtain the filled message queue control body. Because there are many message queue control bodies in the process, when sending information, you need to select one according to demand or agreement (for example, according to the thread ID (equivalent to the identifier of the above-mentioned writer)).
[0271] Step 3.2: Check whether the message queue control body supports the maximum data length of the sent information. If not, a new message queue control body needs to be filled (applied for).
[0272] Step 3.3: Check whether there is any remaining space inside the message queue control body to store the sent data. If not, it is necessary to wait for the release of the storage space for the sent data through a system call.
[0273] Step 3.4: If there is space in the control body, the sent data is copied to the buffer pointed to inside the control body, and the linked list information of the buffer usage in the control body is updated.
[0274] Step 3.5: Notify the receiving end that it can receive data.
[0275] Step 4: Receive data (equivalent to reading the above data).
[0276] Step 4.1, get the filled message queue control body, because there are many message queue control bodies in the process, so when receiving information, you need to select one according to demand or agreement.
[0277] Step 4.2: Check whether there is already message data in the message queue control body. If the data is empty, you need to wait for the message to arrive.
[0278] Step 4.3: Copy the data in the message queue control body to the user buffer, and update the linked list information of the buffer usage in the control body.
[0279] Step 4.4: Notify the sender that data can be sent.
[0280] Based on the above steps 1 to 4, it is possible to use a lock-free message queue to send and receive data.
[0281] Next, we will explain the work that the kernel needs to do with the user when the user mode wants to send or receive data, that is, the processing flow when blocking, including timeout processing.
[0282] refer to Fig.13 As shown in the content, the process may include but is not limited to the following S1301 to S1310.
[0283] S1301. Check whether the sending queue is full.
[0284] If yes, execute the following S1302; if no, write the data.
[0285] S1302: Determine whether the receiving thread has been processed.
[0286] If so, it has been processed and S1305 is executed; if not, it has not been processed and the following S1303 is executed.
[0287] S1303: Determine whether the timeout period has expired and whether the queue is still full.
[0288] If so, that is, the timeout period has expired and the queue is still full, execute S1304; if not, execute S1301.
[0289] S1304: Return error, the queue is still full.
[0290] S1305: The queue is idle, and the data written by the user is returned.
[0291] S1306: Check whether the receiving queue is empty.
[0292] If yes, execute the following S1307; if no, read the data.
[0293] S1307: Determine whether any data has arrived.
[0294] If so, data has arrived, and S1310 is executed; if not, no data has arrived, and S1308 is executed below.
[0295] S1308: Determine whether the timeout period has expired and the queue is still empty.
[0296] If so, that is, the timeout period has expired and the queue is still empty, execute S1309; if not, execute S1306.
[0297] S1309: Return error, the queue is still full.
[0298] S1310. Return the user copy data.
[0299] Using lock-free message queues for multi-threaded communication within a process has the following effects:
[0300] 1. High concurrent performance: Lock-free message queues do not need to use locks to implement concurrent access control. They are suitable for scenarios where push and pop are very frequent, and their efficiency is much higher than mutex. For example, stock quotes can process at least hundreds of thousands of data in one second.
[0301] 2. Low latency: Since the lock-free message queue does not need to wait for the release of the lock, it can reduce the waiting time for communication between threads, thereby reducing the latency of message delivery.
[0302] 3. Avoid deadlock: The lock-free message queue will not have a deadlock caused by lock contention because it does not use locks.
[0303] 4. Reduce programming complexity: Compared with using locks to implement concurrency control, the implementation of lock-free message queues is relatively simple, reducing programming complexity.
[0304] When there are a large number of read and write operations in the real-time system process and the concurrency requirements are high, the use of lock-free message queues can avoid the performance degradation caused by lock competition, thereby improving the system throughput. In addition, the design of lock-free message queues usually optimizes the memory access mode to minimize the competition for memory among multiple threads. Especially in the vehicle operating system, the laser radar sends and receives data frequently, and the radar is responsible for collecting data and is the data sender. Data processing can be placed in a thread according to actual needs.
[0305] For example: multiple producers, the vehicle system is usually equipped with multiple radar sensors (such as forward radar, side radar and rear radar), these sensors may work independently and continuously, generate data and send it at the same time, these sensors can be regarded as multiple producers. One consumer, sometimes, in order to centralize processing and comprehensive analysis, the system design may adopt a processing module (consumer) to process data from multiple radar sensors. This centralized processing can simplify system design, avoid data redundancy, and optimize the execution efficiency of the algorithm.
[0306] Next, the user space is described. When implementing, firstly, a shared memory needs to be applied according to the amount of data sent and received by multiple threads.
[0307] refer to Fig.14As shown in the content, the memory of the lock-free message queue (equivalent to the above-mentioned shared memory) may include but is not limited to: a lock-free message queue control segment 1401 (equivalent to the above-mentioned control information), a ring buffer attribute information 1402 (equivalent to the above-mentioned queue attribute), and a queue buffer area 1403 (equivalent to the above-mentioned storage area).
[0308] The memory of the lock-free message queue may include relevant information of N queues. The relevant information of a queue may include: a lock-free message queue control segment 1401 , a ring buffer attribute information 1402 and a queue buffer area 1403 .
[0309] Next, the lock-free message queue control section 1401 is described.
[0310] The lock-free message queue control section 1401 is equivalent to the above-mentioned message queue control body, and can also be called the control information of the lock-free message queue, which is used to manage the lock-free message queue.
[0311] The lock-free message queue control segment 1401 may include: data head and tail positions 14011 , the number of data waiting to be sent 14012 , the number of data waiting to be received 14013 , the message queue ID 14014 , the maximum supported data length 14015 , the number of ring buffers 14016 , and the ring buffer pointer array 14017 .
[0312] The values of the data head and tail positions 14011 specify the positions of the data to be currently received and sent.
[0313] The number of data waiting to be sent, 14012, is used only when a race condition occurs in the lock-free message queue (race condition means that all buffers are occupied when sending data, or all buffers are empty when receiving data). It is used to indicate how much queue data can be sent at present.
[0314] The number of data waiting to be received, 14013, is also used when a race condition occurs in the lock-free message queue. Its function is to notify the receiving end to receive the data by changing this value after a thread has sent the data.
[0315] Message queue ID 14014 has two functions: one is to indicate its own information, such as user thread ID, queue number, etc., and the other is to manage the data sending and receiving queue when a race occurs.
[0316] The maximum supported data length is 14015, which is specified by the user. This value needs to be assigned according to the actual situation.
[0317] The number of ring buffers 14016 indicates the number of messages supported by the lock-free message queue.
[0318] The ring buffer pointer array 14017 is used to manage the data buffer. The head / tail mentioned above is located in this array.
[0319] Next, the ring buffer attribute information 1402 is described.
[0320] The ring buffer attribute information 1402 may include: whether the buffer is used 14021, the buffer address 14022 and the length of the data in the buffer 14023.
[0321] Whether the buffer is used 14021 is set to be used when sending data and set to be unused after receiving data, which is used to indicate the current buffer usage; the buffer address 4022 is used for data transmission; the data length in the buffer 14023 is used to indicate how much data is in the current buffer.
[0322] Next, the queue buffer area 1403 is described.
[0323] The queue buffer area 1403 is a collection of multiple data storage areas, and all data buffers are managed together. Among them, one queue has one queue buffer area 1403, and one queue buffer area 1403 may include multiple data buffers 14031 (equivalent to the above-mentioned data storage blocks).
[0324] After the memory application is completed, the number of message queue control bodies in the actual process is determined. A single message queue control body is mainly used to manage the data sending and receiving of subsequent threads. After the memory application is successful, the message queue control body is filled and initialized according to actual needs. The initialization content includes setting the buffer for queue reception and transmission, and setting the values of the message queue control body members. It should be noted that the number of messages and the size of the sent and received data in each message queue control body are not necessarily the same, because the situation inside each thread is different. In addition, the ID of the message queue control body needs to be processed by the kernel, which is not repeated here.
[0325] When the user sends data, just send the data directly through the sending function. When sending, you need to select the message control body to be used, and then check whether there is any free area in the message control body. If so, it is necessary to determine whether the length of the sent data is within the length of the free area. If the length of the sent data is greater than the length of the queue data, an error will occur and the sending will fail. Finally, the sent data is copied to the message queue, and the value of tail in the message queue control body is updated. If a receiving race occurs before sending, the receiving end area is directly notified to receive the data. It should be noted that the lock-free message queue uses data structures such as ring buffers to store messages.
[0326] When receiving data in user space multithreading, it is first necessary to check whether the reception is in a race state. If it is in a race state, it will enter a waiting state with a timeout. If the timeout is reached, a reception error will occur. There are two states of receiving race state. One is that the data is still in the sending state and has not been completed; the other is that the data has not been sent to the specified buffer. If the data arrives, the data at the head position is copied from the message queue to the user buffer, and the current buffer is marked as empty. If the sending is in a race state at this time, the sender is notified that the data can be sent.
[0327] It should be noted that the memory barrier function needs to be used when sending and receiving. The role of the memory barrier is to ensure that memory access in multi-threaded programming is performed in the expected order, thereby improving the reliability and correctness of the program. When operating the ring buffer, a memory barrier is required to update the head and tail. Special attention should be paid when sending and receiving data, because access to these functions is multi-threaded, and the order of memory access needs to be controlled to ensure that the memory access order will not be reordered during the data transmission process.
[0328] Next, the kernel space is explained.
[0329] The multi-threaded processing race in user space is implemented through system calls, so the kernel needs to have a corresponding processing mechanism. The kernel will only process when a race occurs, and data is usually sent and received only in user space. When creating a message queue control body in user space, the kernel space also needs to initialize some things. The kernel space needs to process the following: if the sending queue is full during sending, the current information needs to wait for the sending queue to release an idle position; if the parameter passed in by the user space has a timeout, the kernel needs to check whether there is an idle queue within the timeout period, and if not, it needs to return to the user space for processing. Similarly, when receiving data, the kernel needs to handle the situation where the receiving queue is full or the data has not arrived. When the receiving queue is full, it needs to wait for the receiving data queue to be released; the situation where the data has not arrived generally does not occur, because when sending, the sent data will be put into the queue, and then the receiving end will be notified to process the data.
[0330] refer to Fig.15 As shown in the content, the processing process of the kernel space may include but is not limited to the following S1501 to S1512.
[0331] S1501: A race condition occurs in the sending thread.
[0332] S1502: Determine whether the circular buffer is full.
[0333] If yes, execute the following S1503, if no, execute the following S1507.
[0334] S1503, the receiving count is increased by 1.
[0335] S1504: Activate the receiving thread.
[0336] And execute S1509.
[0337] S1505. Sleep the current task and wait for wake-up.
[0338] S1506: Subtract 1 from the sending count.
[0339] S1507: No race condition occurs, and the process is returned to the user for processing.
[0340] S1508: A race condition occurs in the receiving thread.
[0341] S1509: Sleep the current task and wait for wake-up.
[0342] S1510, the sending count is increased by 1.
[0343] S1511. Activate the sending thread.
[0344] And execute S1505.
[0345] S1512, the receiving count is reduced by 1.
[0346] In summary, in the specific implementation process, the user and the kernel need to cooperate with each other to complete the sending and receiving of data. In the user space, no locks are used to control data access, so that multi-threaded access to data is fast and efficient.
[0347] In a third aspect, the present application provides a first communication device. The first communication device is deployed at a first writing party in a communication system, the communication system includes N writing parties and one reading party, N is an integer greater than 1; the first writing party is any writing party among the N writing parties; Fig.16 As shown, the first communication device 160 includes a first determining unit 1601 , a writing unit 1602 , and a first updating unit 1603 .
[0348] The first determining unit 1601 is used to determine a first message queue control body of a first writer among N message queue control bodies included in a shared memory of a communication system; one writer corresponds to one message queue control body.
[0349] The writing unit 1602 is used to write the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area.
[0350] The first updating unit 1603 is used to update the queue attribute of the first message queue control body; wherein, in the queue attribute of the updated first message queue control body, the state of each first data storage block in the at least one first data storage block is in use state.
[0351] In some embodiments, a queue message control body includes: a queue storage area, queue attributes and queue control information; the queue storage area includes multiple data storage blocks; the queue attributes include one or more of the following: the status of each data storage block in the multiple data storage blocks, the address of each of the data storage blocks, and the data length in each data storage block; the queue control information includes one or more of the following: a position pointer of the queue storage area, the number of written data, the number of read data, the identifier of the writer, the maximum supported data length, the number of data storage blocks included in the queue storage area, and a position pointer array of the queue storage area.
[0352] In some embodiments, the first determination unit 1601 is also used to: search for the identifier of the writer of each message queue control body in multiple message queue control bodies; search for the identifier of the first writer in the identifier of the writer of each message queue control body, and determine the message queue control body to which the first writer identifier belongs as the first message queue control body.
[0353] In some embodiments, the first communication device 160 may also include a first processing unit, which is used to execute the following before the write unit 1602 executes the writing of the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body: checking whether there is an idle data storage block in the queue storage area of the first message queue control body; performing a first processing when there is no idle data storage block in the queue storage area of the first message queue control body; until there is an idle data storage block in the queue storage area of the first message queue control body; and determining the idle data storage block as at least one first data storage block when there is an idle data storage block in the queue storage area of the first message queue control body.
[0354] In some embodiments, the first processing unit is also used to execute: when there is no idle data storage block in the queue storage area of the first message queue control body, determine that the queue storage area of the first message queue control body is in a sending contention state; enter the kernel state, and in the kernel state, send a first read instruction to the reader so that the reader reads the data in the queue storage area of the first message queue control body; and return to the user state for processing.
[0355] In some embodiments, the first processing unit is further configured to execute: in kernel mode, sending a first read instruction to a reader so that the reader reads data from a queue storage area of the first message queue control body; if a data storage block in an idle state is obtained based on the instruction of the first read instruction, returning to the user mode to write the first data; if a data storage block in an idle state is not obtained based on the instruction of the first read instruction, returning to the user mode to wait;
[0356] Alternatively, in kernel mode, a first read instruction is sent to a reader within a first time so that the reader reads data from a queue storage area of a first message queue control body; if a data storage block in an idle state is obtained based on the indication of the first read instruction within the first time, the process returns to user mode to write the first data; or, if a data storage block in an idle state is not obtained based on the indication of the first read instruction after the first time is reached, the process returns to user mode to wait.
[0357] In some embodiments, the writing unit 1602 is further configured to execute:
[0358] Obtain control information of a first message queue control body; determine whether the maximum supported data length in the control information meets the requirements of the first data; if the maximum supported data length does not meet the requirements of the first data, execute writing of the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body; if the maximum supported data length does not meet the requirements of the first data, obtain a newly applied first message queue control body, and write the first data into at least one first data storage block in the queue storage area of the new first message queue control body; wherein the maximum supported data length in the control information in the new first message queue control body meets the requirements of the first data.
[0359] In some embodiments, the first communication device 160 may further include an instruction processing unit, the instruction processing unit being configured to execute:
[0360] When the queue attributes of the first message queue control body are updated, a second read instruction is sent to the reader, and the second read instruction is used to instruct the reader to read the first data; or, when a sending contention occurs in the queue storage area in the first message queue control body, a first read instruction is sent to the reader, and the first read instruction is used to instruct the reader to read the data in the queue storage area.
[0361] In some embodiments, the queue storage area is a circular queue storage area.
[0362] In a fourth aspect, the present application provides a first communication device. The present application also provides a second communication device, which is deployed at a reader in a communication system, wherein the communication system includes N writers and one reader, where N is an integer greater than 1.
[0363] refer to Fig.17 As shown in the content, the second communication device 170 may include an acquiring unit 1701 , a second determining unit 1702 , a reading unit 1703 and a second updating unit 1704 .
[0364] The acquisition unit 1701 is used to acquire N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body;
[0365] The second determining unit 1702 is used to determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located;
[0366] A reading unit 1703 is used to read second data from at least one second data storage block in a queue storage area of a second message queue control body; the queue storage area is a lock-free queue storage area;
[0367] The second updating unit 1704 is configured to update the queue attributes of the second message queue control body, wherein in the updated queue attributes of the second message queue control body, the state of each second data storage block in the at least one second data storage block is an idle state.
[0368] In some embodiments, the second determination unit 1702 is further configured to: determine the writer to which the second data belongs; and determine the second message queue control body among multiple message queue control bodies based on the identifier of the writer to which the second data belongs.
[0369] In some embodiments, the second communication device 170 may further include a second processing unit, the second processing unit being configured to execute, in the reading unit 1703, reading the second data from at least one second data storage block in the queue storage area of the second message queue control body; before the queue storage area is a lock-free queue storage area, execute:
[0370] Check whether there is a data storage block in use in the queue storage area of the second message queue control body; if there is no data storage block in use in the queue storage area of the second message queue control body, perform the second processing until there is a data storage block in use in the queue storage area of the second message queue control body; if there is a data storage block in use in the queue storage area of the second message queue control body, determine that the data storage block in use is the second data storage block.
[0371] In some embodiments, the second processing unit is also used to execute: when there is no data storage block in use in the queue storage area of the second message queue control body, determine that the queue storage area is in a receive contention state; enter the kernel state, and in the kernel state, send a first write instruction to the writer corresponding to the second message queue control body to write the second data, and return to the user state for processing.
[0372] In some embodiments, the second processing unit is further used to execute: in kernel mode, sending a first write instruction to a writer corresponding to the second message queue control body to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction; then returning to user mode to read the second data; if a data storage block in use is not obtained based on the instruction of the first instruction, then returning to user mode to wait;
[0373] Alternatively, in kernel state, a first write instruction is sent to the writer corresponding to the second message queue control body within a second time to write the first data; if a data storage block in use is obtained based on the instruction of the first write instruction within the second time, then the process returns to user state to read the second data; or, after the second time is reached, if a data storage block in use is not obtained based on the instruction of the first instruction, then the process returns to user state to wait.
[0374] In some embodiments, the second communication device 170 may further include a write instruction processing unit, the write instruction processing unit being configured to execute:
[0375] When the queue attributes of the second message queue control body are updated, a second write instruction is sent to the reader corresponding to the second message queue control body, and the second write instruction is used to instruct the reader corresponding to the second message queue control body to write new data; or, when a receive contention occurs in the queue storage area in the second message queue control body, a first write instruction is sent to the reader corresponding to the second message queue control body, and the second instruction is used to instruct the reader corresponding to the second message queue control body to write the second data.
[0376] In some embodiments, the second communication device 170 may further include an application unit, which is used to execute: determining the shared memory of the communication system; applying for N message queue control bodies in the shared memory; one writer corresponds to one message queue control body; and initializing the N message queue control bodies based on the needs of the N writers.
[0377] In a fifth aspect, the present application further provides a communication system, the communication system comprising N writers and one reader, where N is an integer greater than 1;
[0378] Any one of the N writers is used to:
[0379] A first message queue control body of a first writer is determined in N message queue control bodies included in a shared memory of a communication system; one writer corresponds to one message queue control body; first data of the first writer is written into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; queue properties of the first message queue control body are updated; wherein, in the queue properties of the updated first message queue control body, the state of each first data storage block in at least one first data storage block is a use state; the first writer is any writer among the N writers;
[0380] The reader is used to:
[0381] Acquire N message queue control bodies included in the shared memory of the communication system; determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located; read the second data from at least one second data storage block in the queue storage area of the second message queue control body; update the queue attributes of the second message queue control body, wherein, in the queue attributes of the updated second message queue control body, the state of each second data storage block in at least one second data storage block is an idle state.
[0382] In some embodiments, multiple writers among the N writers are used to perform in parallel:
[0383] A first message queue control body of a first writer is determined among N message queue control bodies included in a shared memory of a communication system; one writer corresponds to one message queue control body; first data of the first writer is written into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; queue properties of the first message queue control body are updated; wherein, in the queue properties of the updated first message queue control body, the state of each first data storage block in at least one first data storage block is a use state; the first writer is any writer among the multiple writers.
[0384] In a sixth aspect, the present application further provides a storage medium storing a computer program or instruction, which, when executed by a processor, implements the communication method provided in the first or second aspect above.
[0385] In a seventh aspect, the present application further provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a processor, the communication method provided in the first or second aspect above is implemented.
[0386] It should be noted here that the description of the above storage medium, system, device and program product embodiments is similar to the description of the above method embodiment, and has similar beneficial effects as the method embodiment. For technical details not disclosed in the storage medium, system, device and program product embodiments of this application, please refer to the description of the method embodiment of this application for understanding.
[0387] It should be understood that "one embodiment" or "an embodiment" mentioned throughout the specification means that specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in some embodiments" appearing throughout the specification may not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. It should be understood that in various embodiments of the present application, the size of the sequence number of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present application. The above-mentioned sequence numbers of the embodiments of the present application are for description only and do not represent the advantages and disadvantages of the embodiments.
[0388] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0389] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0390] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units; they may be located in one place or distributed on multiple network units; some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0391] In addition, all functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may be separately configured as a unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0392] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiment; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM), magnetic disks or optical disks, etc., various media that can store program codes.
[0393] Alternatively, if the above-mentioned integrated unit of the present application is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application can essentially or in other words, the part that contributes to the relevant technology can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the methods of each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks, or optical disks.
[0394] The above description is only an implementation method of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A communication method, characterized in that: The method is applied to a first writer in a communication system, the communication system comprising N writers and one reader, wherein N is an integer greater than 1; the first writer is any one of the N writers; the method comprises: Determine a first message queue control body of the first writer among N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body; Writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; The queue attribute of the first message queue control body is updated; wherein, in the updated queue attribute of the first message queue control body, the state of each of the at least one first data storage block is in use state.
2. The method according to claim 1, characterized in that: A queue message control body includes: a queue storage area, queue attributes and queue control information; The queue storage area includes a plurality of data storage blocks; The queue attribute includes one or more of the following: a state of each of the plurality of data storage blocks, an address of each of the data storage blocks, and a length of data in each of the data storage blocks; The queue control information includes one or more of the following: a location pointer of the queue storage area, the number of written data, the number of read data, an identifier of the writer, a maximum supported data length, the number of data storage blocks included in the queue storage area, and an array of location pointers of the queue storage area.
3. The method according to claim 1, characterized in that: In a case where the queue control information in the first message queue control body includes an identifier of a writer, determining the message queue control body of the first writer among N message queue control bodies included in the shared memory of the communication system includes: Finding the identifier of the writer of each of the message queue control bodies in the plurality of message queue control bodies; The identifier of the first writer is searched in the identifiers of the writers of each of the message queue control bodies, and the message queue control body to which the identifier of the first writer belongs is determined as the first message queue control body.
4. The method according to any one of claims 1 to 3, characterized in that: Before writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body, the method further includes: Check whether there is an idle data storage block in the queue storage area of the first message queue control body; In the case that there is no idle data storage block in the queue storage area of the first message queue control body, a first process is performed until there is an idle data storage block in the queue storage area of the first message queue control body; When there is an idle data storage block in the queue storage area of the first message queue control body, the idle data storage block is determined as the at least one first data storage block.
5. The method according to claim 4, characterized in that In the case that there is no idle data storage block in the queue storage area of the first message queue control body, performing a first process includes: When there is no idle data storage block in the queue storage area of the first message queue control body, determining that the queue storage area of the first message queue control body is in a sending contention state; Enter the kernel state, and in the kernel state, send a first read instruction to the reader so that the reader reads the data in the queue storage area of the first message queue control body; and return to the user state for processing.
6. The method according to claim 5, characterized in that The step of entering the kernel state and sending a first read instruction to the reader in the kernel state so that the reader reads data from a queue storage area of the first message queue control body and returns to the user state for processing includes: In the kernel state, sending a first read instruction to the reader so that the reader reads data from the queue storage area of the first message queue control body; If a data storage block in an idle state is obtained based on the instruction of the first read instruction, returning to the user state to write the first data; if a data storage block in an idle state is not obtained based on the instruction of the first read instruction, returning to the user state to wait; or, In the kernel state, sending a first read instruction to the reader within a first time, so that the reader reads data from a queue storage area of the first message queue control body; If a data storage block in an idle state is obtained based on the indication of the first read instruction within the first time, the system returns to the user state to write the first data; or, after the first time is reached, if a data storage block in an idle state is not obtained based on the indication of the first read instruction, the system returns to the user state to wait.
7. The method according to any one of claims 1 to 3, characterized in that: The step of writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body comprises: Obtaining control information of the first message queue control body; Determining whether the maximum supported data length in the control information meets the requirements of the first data; In a case where the maximum supported data length does not meet the requirements of the first data, executing the step of writing the first data of the first writer into at least one first data storage block in the queue storage area of the first message queue control body; When the maximum supported data length does not meet the requirements of the first data, a new first message queue control body is obtained and the first data is written into at least one first data storage block in the queue storage area of the new first message queue control body; Among them, the maximum supported data length in the control information in the new first message queue control body meets the requirements of the first data.
8. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: When the updating of the queue attribute of the first message queue control body is completed, a second read instruction is sent to the reader, where the second read instruction is used to instruct the reader to read the first data; or, In the case where a sending contention occurs in the queue storage area in the first message queue control body, a first reading instruction is sent to the reader, where the first reading instruction is used to instruct the reader to read data from the queue storage area.
9. The method according to claim 1, characterized in that: The queue storage area is a ring queue storage area.
10. A communication method, characterized in that: The method is applied to a reader in a communication system, the communication system comprising N writers and one reader, wherein N is an integer greater than 1; the method comprises: Acquire N message queue control bodies included in the shared memory of the communication system; one of the writing parties corresponds to one of the message queue control bodies; Determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located; Reading the second data in at least one second data storage block in a queue storage area of the second message queue control body; the queue storage area is a lock-free queue storage area; The queue attribute of the second message queue control body is updated, wherein in the updated queue attribute of the second message queue control body, the state of each of the at least one second data storage block is an idle state.
11. The method according to claim 10, characterized in that The step of determining, among the N message queue control bodies, a second message queue control body where the second data to be read is located, comprises: Determining a writer of the second data; Among the multiple message queue control bodies, the second message queue control body is determined based on the identifier of the writer to whom the second data belongs.
12. The method according to claim 10, characterized in that Before executing the step of reading the second data from at least one second data storage block in the queue storage area of the second message queue control body, the method further comprises: Check whether there is a data storage block in use in the queue storage area of the second message queue control body; In the case that the queue storage area of the second message queue control body does not have a data storage block in use, performing a second process until the queue storage area of the second message queue control body has a data storage block in use; When there is a data storage block in use in the queue storage area of the second message queue control body, the data storage block in use is determined to be the second data storage block.
13. The method according to claim 12, characterized in that The second processing is performed when there is no data storage block in use in the queue storage area of the second message queue control body, including: In the case where there is no data storage block in use in the queue storage area of the second message queue control body, determining that the queue storage area is in a receiving contention state; Enter the kernel state, and in the kernel state, send a first write instruction to the writing party corresponding to the second message queue control body to write the second data, and return to the user state for processing.
14. The method according to claim 13, characterized in that The step of sending a first write instruction to a writer corresponding to the second message queue control body in the kernel state to write the first data, and returning to the user state for processing includes: In the kernel state, sending the first write instruction to a writing party corresponding to the second message queue control body to write the first data; If a data storage block in use is obtained based on the instruction of the first write instruction, the system returns to the user state to read the second data; if a data storage block in use is not obtained based on the instruction of the first instruction, the system returns to the user state to wait; or, In the kernel state, sending the first write instruction to a writing party corresponding to the second message queue control body within a second time to write the first data; If a data storage block in use is obtained based on the instruction of the first write instruction within the second time, the program returns to the user state to read the second data; or, after the second time is reached, if a data storage block in use is not obtained based on the instruction of the first instruction, the program returns to the user state to wait.
15. The method according to claim 10, characterized in that The method further comprises: When the updating of the queue attribute of the second message queue control body is completed, a second write instruction is sent to a reader corresponding to the second message queue control body, where the second write instruction is used to instruct the reader corresponding to the second message queue control body to write new data; or, When a receive contention occurs in the queue storage area in the second message queue control body, a first write instruction is sent to a reader corresponding to the second message queue control body, and the second instruction is used to instruct the reader corresponding to the second message queue control body to write the second data.
16. The method according to claim 10, characterized in that Before acquiring the N message queue control bodies included in the shared memory of the communication system, the method further includes: determining a shared memory of the communication system; Applying for N message queue control bodies in the shared memory; one of the writing parties corresponds to one of the message queue control bodies; Based on the requirements of the N writers, the N message queue control bodies are initialized.
17. A first communication device, characterized in that: The first communication device is deployed at a first writer in a communication system, the communication system comprising N writers and one reader, where N is an integer greater than 1; The first writer is any one of the N writers; The first communication device comprises: A first determining unit, configured to determine a first message queue control body of the first writer among the N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body; A writing unit, configured to write the first data of the first writer into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; The first updating unit is used to update the queue attribute of the first message queue control body; wherein, in the queue attribute of the updated first message queue control body, the state of each of the at least one first data storage block is in use state.
18. A second communication device, characterized in that: The second communication device is deployed on a reader in a communication system, the communication system includes N writers and one reader, where N is an integer greater than 1; the second communication device includes: An acquisition unit, used for acquiring N message queue control bodies included in the shared memory of the communication system; one of the writing parties corresponds to one of the message queue control bodies; A second determining unit, configured to determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located; A reading unit, configured to read the second data from at least one second data storage block in a queue storage area of the second message queue control body; the queue storage area is a lock-free queue storage area; The second updating unit is used to update the queue attribute of the second message queue control body, wherein in the updated queue attribute of the second message queue control body, the state of each of the at least one second data storage block is an idle state.
19. A communication system, characterized in that: The communication system includes N writers and one reader, where N is an integer greater than 1; Any of the N writing parties is used for: Determine a first message queue control body of a first writer among the N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body; write first data of the first writer into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; update the queue attribute of the first message queue control body; wherein, in the updated queue attribute of the first message queue control body, the state of each of the at least one first data storage block is a use state; the first writer is any writer among the N writers; The reader is used to: Acquire N message queue control bodies included in the shared memory of the communication system; determine, among the N message queue control bodies, a second message queue control body where the second data to be read is located; read the second data in at least one second data storage block in the queue storage area of the second message queue control body; update the queue attributes of the second message queue control body, wherein, in the updated queue attributes of the second message queue control body, the state of each of the at least one second data storage block is an idle state.
20. The system according to claim 19, characterized in that A plurality of the N writers are used to execute in parallel: A first message queue control body of a first writer is determined among N message queue control bodies included in the shared memory of the communication system; one writer corresponds to one message queue control body; first data of the first writer is written into at least one first data storage block in a queue storage area of the first message queue control body; the queue storage area is a lock-free queue storage area; queue properties of the first message queue control body are updated; wherein, in the updated queue properties of the first message queue control body, the status of each of the at least one first data storage block is a use status; the first writer is any writer among the multiple writers.
21. A computer-readable storage medium, characterized in that: The storage medium stores a computer program or an instruction, and when the computer program or the instruction is executed by the processor, the method according to any one of claims 1 to 16 is implemented.
22. A computer program product, characterized in that The computer program product comprises a computer program or instructions, and when the computer program or instructions are executed by a processor, the method according to any one of claims 1 to 16 is implemented.
Citation Information
Cited By
Data processing method and device, computer equipment, readable storage medium and program product
CN120315910A
