CN108710531B - Data writing method and device of circular queue, terminal equipment and storage medium - Google Patents
Data writing method and device of circular queue, terminal equipment and storage medium Download PDFInfo
- Publication number
- CN108710531B CN108710531B CN201810358392.7A CN201810358392A CN108710531B CN 108710531 B CN108710531 B CN 108710531B CN 201810358392 A CN201810358392 A CN 201810358392A CN 108710531 B CN108710531 B CN 108710531B
- Authority
- CN
- China
- Prior art keywords
- pointer
- circular queue
- data
- thread
- writing data
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/48—Program initiating; Program switching, e.g. by interrupt
- G06F9/4806—Task transfer initiation or dispatching
- G06F9/4843—Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
- G06F9/4881—Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/546—Message passing systems or structures, e.g. queues
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2209/00—Indexing scheme relating to G06F9/00
- G06F2209/54—Indexing scheme relating to G06F9/54
- G06F2209/548—Queue
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Memory System Of A Hierarchy Structure (AREA)
Abstract
The invention relates to the technical field of data storage, and provides a data writing method and device of a circular queue, terminal equipment and a computer storage medium. The data writing method comprises the following steps: when any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present; if other threads are writing data into the circular queue at present, returning to execute the step of judging whether other threads are writing data into the circular queue at present and the subsequent steps after the preset time length; if no other thread writes data into the circular queue at present, each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer, and the first pointer points to the position of the next writable data in the circular queue; and writing data into the circular queue by the thread which successfully competes for the first pointer. By adopting the method, the data writing efficiency of the circular queue can be effectively improved.
Description
Technical Field
The present invention relates to the field of data storage technologies, and in particular, to a method and an apparatus for writing data into a circular queue, a terminal device, and a computer storage medium.
Background
Logically connecting the head and tail of the array will form a circular queue. The lock-free circular queue implements a mechanism that allows a user to write data circularly to the queue without regard to the queue fullness.
At present, the data writing method of the lock-free circular queue generally comprises the following steps: each thread uses cas (compare and swap), that is, the atomic operation mode of the compare and swap mechanism competes and advances the first pointer (the first pointer points to the next data-writable position in the queue), if the competition fails, the competition continues, if the competition succeeds, the corresponding writing position is obtained, and then the corresponding data is written into the queue. After writing the data to the queue, the respective threads again use the atomic operation mode of the CAS mechanism to compete for and advance the second pointer (the second pointer points to the next position of the maximum position in the queue from which the data can be read), indicating that the write operation of the data is complete when the second pointer is advanced to the same position as the first pointer.
In the above method, when a plurality of threads write data simultaneously, two contention operations of the CAS mechanism are required, and each thread repeats contention after the contention fails, which results in inefficient writing of data.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for writing data into a circular queue, a terminal device, and a computer storage medium, which can improve data writing efficiency of the circular queue.
A first aspect of an embodiment of the present invention provides a data writing method for a circular queue, including:
when any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present;
if other threads are writing data into the circular queue at present, returning to execute the step of judging whether other threads are writing data into the circular queue at present and the subsequent steps after the preset time length;
if no other thread writes data into the circular queue at present, each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer, and the first pointer points to the position of the next writable data in the circular queue;
and writing data into the circular queue by the thread which successfully competes for the first pointer.
A second aspect of the embodiments of the present invention provides a data writing device for a circular queue, including:
the first judgment module is used for judging whether other threads write data into the circular queue at present or not when any thread is ready to write the data into the circular queue;
the delay module is used for returning to execute the step of judging whether other threads write data into the circular queue or not and the subsequent steps after the preset time length if other threads write data into the circular queue currently;
a competition pointer module, configured to, if no other thread is currently writing data into the circular queue, each thread uses an atomic operation mode of a compare-and-exchange mechanism to compete for a first pointer, where the first pointer points to a next position in the circular queue, where the data can be written;
and the data writing module is used for writing the data into the circular queue by the thread which successfully competes for the first pointer.
A third aspect of the present invention provides a terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the steps of the data writing method for a circular queue according to the first aspect of the present invention when executing the computer program.
A fourth aspect of the embodiments of the present invention provides a computer-readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the computer program implements the steps of the data writing method for a circular queue according to the first aspect of the embodiments of the present invention.
In the embodiment of the present invention, when a certain thread prepares to write data into a circular queue, it needs to be determined whether other threads are currently writing data into the circular queue; if other threads are writing data into the circular queue at present, the threads are in a waiting state until no threads are writing data into the circular queue at present; then, each thread uses the atomic operation mode of the CAS mechanism to compete for the first pointer, and the thread which successfully competes for the first pointer writes data into the circular queue. The steps limit that only one thread can write data into the circular queue at the same time, the whole process only comprises one competition operation of a CAS mechanism, and the data writing efficiency of the circular queue is effectively improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
Fig. 1a is a flowchart of a first embodiment of a method for writing data into a circular queue according to an embodiment of the present invention;
FIG. 1b is a schematic diagram of a circular queue in the data writing method of the circular queue shown in FIG. 1 a;
FIG. 2 is a flowchart illustrating a method for writing data into a circular queue according to a second embodiment of the present invention;
fig. 3 is a flowchart of a data writing method for a circular queue according to a third embodiment of the present invention;
fig. 4 is a structural diagram of an embodiment of a data writing device of a circular queue according to an embodiment of the present invention;
fig. 5 is a schematic diagram of a terminal device according to an embodiment of the present invention.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system structures, techniques, etc. in order to provide a thorough understanding of the embodiments of the invention. It will be apparent, however, to one skilled in the art that the present invention may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present invention with unnecessary detail.
The embodiment of the invention provides a data writing method and device of a circular queue, terminal equipment and a computer storage medium, which can improve the data writing efficiency of the circular queue.
Referring to fig. 1a, a first embodiment of a method for writing data into a circular queue according to the present invention includes:
101. when any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present;
in the embodiment of the present invention, when any one thread is ready to write data into the circular queue, it is first determined whether there are other threads writing data into the circular queue at the current time. If there are other threads currently writing data into the circular queue, execute step 102; if no other threads are currently writing data to the circular queue, step 103 is performed.
A schematic structural diagram of a circular queue according to an embodiment of the present invention is shown in fig. 1b, where each square in the diagram indicates a position of the circular queue (only a part of the position of the circular queue is shown in the diagram), a blank square indicates a position where data can be written, a cross square indicates a position where data can be read, and reference numbers of the positions sequentially increase from left to right. WI represents a first pointer pointing to the next location in the circular queue where data can be written; MI represents a second pointer pointing to the next position of the maximum position of readable data in the circular queue (i.e. the position of the readable data with the largest label, the 3 rd cross square in the figure); RI denotes a third pointer pointing to the position of the next read data in the circular queue. If the first pointer and the second pointer point to the same position, indicating that no data is currently written into the circular queue; otherwise, if the first pointer and the second pointer do not point to the same position, it indicates that there is data currently being written into the circular queue, and the second pointer will not advance to the same position as the first pointer until all data is written.
Further, step 101 may include:
(1) judging whether the first pointer and the second pointer point to the same position or not;
(2) if the first pointer and the second pointer point to the same position, judging that no other thread writes data into the circular queue currently; and if the first pointer and the second pointer do not point to the same position, judging that other threads write data into the circular queue currently.
For the judgment basis of the above steps (1) and (2), the structure description of the circular queue can be referred to.
102. Delaying for a preset duration;
and currently, other threads write data into the circular queue, delay the preset time at the moment, and then return to the step 101 to execute the next judgment. That is, when a thread is ready to write data into the circular queue, if another thread is currently writing data into the circular queue, the thread is in a waiting state. By means of the setting, only one thread can write data into the circular queue at the same time.
103. Each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer;
no thread is currently writing data to the circular queue, and each thread competes for the first pointer using an atomic operation of the compare-and-swap mechanism. The threads are all threads currently preparing to write data to the circular queue, and they compete for the operation right of the first pointer by adopting an atomic operation mode of a comparison and exchange mechanism.
104. And writing data into the circular queue by the thread which successfully competes for the first pointer.
At the same time, only one thread can compete successfully, and at the moment, the thread which competes successfully writes data into the circular queue. Then, it can return to step 101 to repeatedly execute steps 101 and 104 until the data write operations of all threads are completed.
Further, step 104 may include:
(1) advancing the first pointer from a current position to a next position;
(2) writing data to the current position by the thread which successfully competes for the first pointer;
(3) advancing the second pointer to a location pointed to by the first pointer.
For the above steps (1) to (3), after the thread which competes successfully for the first pointer obtains the operation right of the first pointer, the thread advances the first pointer from the current position to the next position where data can be written. And then the thread writes the data to be written into the current position, and after the data writing is finished, the second pointer is pushed to the position pointed by the first pointer, so that the complete data writing operation is finished.
In the embodiment of the present invention, when a certain thread prepares to write data into a circular queue, it needs to be determined whether other threads are currently writing data into the circular queue; if other threads are writing data into the circular queue at present, the threads are in a waiting state until no threads are writing data into the circular queue at present; then, each thread uses the atomic operation mode of the CAS mechanism to compete for the first pointer, and the thread which successfully competes for the first pointer writes data into the circular queue. The steps limit that only one thread can write data into the circular queue at the same time, the whole process only comprises one competition operation of a CAS mechanism, and the data writing efficiency of the circular queue is effectively improved.
Referring to fig. 2, a second embodiment of a method for writing data into a circular queue according to the present invention includes:
201. when any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present;
when any thread is ready to write data into the circular queue, it is determined whether another thread is currently writing data into the circular queue, and the specific determination method may refer to the related description of step 101. If there are other threads currently writing data into the circular queue, execute step 202; if no other threads are currently writing data to the circular queue, step 205 is performed.
202. Judging whether the thread which is currently writing data into the circular queue is abnormal or not;
and currently, other threads write data into the circular queue, and whether the threads currently writing data into the circular queue generate abnormity is judged. While the thread may be crashed during the execution process, if some thread generates an exception during the process of writing data into the circular queue, the data writing process is interrupted, and at this time, new data enqueue cannot be executed. Generally, a timing method may be adopted to determine whether a thread currently writing data into the circular queue generates an exception, for example, a time duration is set, and if a thread still does not complete data writing operation within the time duration, it may be determined that the thread generates an exception. If the thread currently writing data into the circular queue is abnormal, go to step 203; if no exception is generated in the thread currently writing data into the circular queue, step 204 is executed.
203. Using an atomic operation mode of a compare-and-swap mechanism to advance the second pointer to a location pointed to by the first pointer;
the thread currently writing data into the circular queue generates an exception, and the atomic operation mode of the compare-and-swap mechanism is used to push the second pointer to the position pointed by the first pointer, where the first pointer and the second pointer refer to the related description of the first embodiment of the present invention. That is, the current write location is abandoned, the data write process of the thread is forcibly ended, and the queue is advanced to the next location, so that other threads can normally compete and perform the enqueue operation of data. Then, step 204 may be performed, and step 205 may also be performed.
204. Delaying for a preset duration;
delaying for a preset time period and then returning to step 201.
205. Each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer;
206. and writing data into the circular queue by the thread which successfully competes for the first pointer.
The steps 205-206 are the same as the steps 103-104, and the related description of the steps 103-104 can be referred to.
Compared with the first embodiment of the present invention, in this embodiment, it is determined whether an exception occurs in a thread that is currently writing data into the circular queue, and after the exception is determined to occur, the second pointer is pushed to the position pointed by the first pointer by using an atomic operation manner of a compare-and-swap mechanism, so as to forcibly end the data writing process of the thread, thereby ensuring that the data queuing process is not blocked by an exception thread, and avoiding the problem that data cannot be queued.
Referring to fig. 3, a third embodiment of a method for writing data into a circular queue according to the present invention includes:
301. initializing a count value of a counter;
a counter is set to initialize a count value of 0.
Further, before step 301, the method may further include:
and recording the target position pointed by the first pointer.
Specifically, the target position may be stored in a register.
302. When any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present;
when any thread is ready to write data into the circular queue, whether other threads are currently writing data into the circular queue is judged. If there are other threads currently writing data into the circular queue, execute step 303; if no other threads are currently writing data to the circular queue, go to step 307.
303. Judging whether the count value reaches a first threshold value;
and currently, other threads write data into the circular queue, and whether the count value reaches a first threshold value is judged. The first threshold may be set according to actual conditions, for example, 1000 times. The count value represents the maximum number of retries for thread enqueuing, and if the count value reaches the first threshold value, it indicates that an exception occurs in the thread currently writing data into the circular queue, step 304 is executed, otherwise step 305 is executed.
304. Using an atomic operation mode of a compare-and-swap mechanism to advance the second pointer to a location pointed to by the first pointer;
step 304 is the same as step 203, and the related description of step 203 can be specifically referred to, and after step 304, step 307 is executed.
Further, if the target location pointed to by the first pointer is recorded before step 301, in step 304, when it fails to advance the second pointer to the location pointed to by the first pointer using the atomic operation mode of the compare-and-swap mechanism, the step of recording the target location pointed to by the first pointer is returned.
305. Accumulating the count value once;
the count value does not reach the first threshold value, at which point the count value is accumulated once, and then step 306 is performed.
Further, if the target location pointed to by the first pointer is recorded before step 301, before step 305, the method may further include:
(1) judging whether the first pointer points to the target position currently;
(2) if the first pointer currently points to the target location, go to step 305; and if the first pointer does not point to the target position currently, returning to the step of recording the target position pointed by the first pointer.
For the above steps (1) to (2), before accumulating the count value, it is determined whether the first pointer currently points to the target position. If the first pointer currently points to the target position, the queue is not advanced, and the count value of the counter is incremented at the moment. If the first pointer does not currently point to the target location, it indicates that the location of the first pointer has shifted, i.e. the queue has been advanced, and then the step of recording the target location pointed to by the first pointer is returned to prepare for performing the next data enqueue operation.
306. Delaying for a preset duration;
delaying for a preset duration and then returning to step 302.
307. Each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer;
308. and writing data into the circular queue by the thread which successfully competes for the first pointer.
The steps 307-308 are the same as the steps 103-104, and the related description of the steps 103-104 can be referred to.
Compared with the first embodiment of the present invention, in this embodiment, a counter is set, whether the thread currently writing data into the circular queue is abnormal is determined according to whether the counter reaches a preset threshold, and after it is determined that an abnormality occurs, the second pointer is pushed to the position pointed by the first pointer by using an atomic operation mode of a compare-and-swap mechanism, so as to forcibly end the data writing process of the thread, thereby ensuring that the process of enqueuing data is not blocked by an abnormal thread, and avoiding the problem that data cannot be enqueued.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present invention.
The above mainly describes a data writing method for a circular queue, and a data writing apparatus for a circular queue will be described below.
Referring to fig. 4, an embodiment of a circular queue data writing apparatus according to the present invention includes:
a first determining module 401, configured to determine whether another thread is currently writing data into the circular queue when any thread is ready to write data into the circular queue;
a delay module 402, configured to, if there is another thread currently writing data into the circular queue, return to perform the step of determining whether there is another thread currently writing data into the circular queue and subsequent steps after a preset time length;
a competition pointer module 403, configured to, if no other thread is currently writing data into the circular queue, each thread uses an atomic operation mode of a compare-and-swap mechanism to compete for a first pointer, where the first pointer points to a next position in the circular queue, where data can be written;
a data writing module 404, configured to write data into the circular queue by a thread that successfully competes for the first pointer.
Further, the data writing device of the circular queue may further include:
a first determining module, configured to determine that no other thread is currently writing data into the circular queue if the first pointer and the second pointer point to a same position, where the second pointer points to a next position of a maximum position of readable data in the circular queue;
the second judgment module is used for judging that other threads write data into the circular queue currently if the first pointer and the second pointer do not point to the same position;
the data writing module comprises:
a first pointer advancing unit for advancing the first pointer from a current position to a next position;
the data writing unit is used for writing data to the current position by a thread which successfully competes for the first pointer;
and the second pointer advancing unit is used for advancing the second pointer to the position pointed by the first pointer.
Further, the data writing device of the circular queue may further include:
the second judgment module is used for judging whether the thread which is currently writing data into the circular queue is abnormal or not;
and the pushing module is used for pushing the second pointer to the position pointed by the first pointer by using an atomic operation mode of a comparison and exchange mechanism if the thread which is currently writing data into the circular queue generates an exception.
The embodiment of the present invention further provides a terminal device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the steps of the data writing method for the circular queue as shown in any one of fig. 1a to fig. 3 when executing the computer program.
An embodiment of the present invention further provides a computer-readable storage medium, where a computer program is stored, and when the computer program is executed by a processor, the computer program implements the steps of the data writing method for a circular queue shown in any one of fig. 1a to fig. 3.
Fig. 5 is a schematic diagram of a terminal device according to an embodiment of the present invention. As shown in fig. 5, the terminal device 5 of this embodiment includes: a processor 50, a memory 51 and a computer program 52 stored in said memory 51 and executable on said processor 50. The processor 50 executes the computer program 52 to implement the steps of the above-mentioned embodiments of the data writing method for each circular queue, such as the steps 101 to 104 shown in fig. 1 a. Alternatively, the processor 50, when executing the computer program 52, implements the functions of each module/unit in the above-mentioned device embodiments, for example, the functions of the modules 401 to 404 shown in fig. 4.
The computer program 52 may be divided into one or more modules/units, which are stored in the memory 51 and executed by the processor 50 to accomplish the present invention. The one or more modules/units may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution process of the computer program 52 in the terminal device 5.
The terminal device 5 may be various types of computing devices such as a mobile phone, a desktop computer, a notebook, a palm computer, and a cloud server. The terminal device may include, but is not limited to, a processor 50, a memory 51. It will be understood by those skilled in the art that fig. 5 is only an example of the terminal device 5, and does not constitute a limitation to the terminal device 5, and may include more or less components than those shown, or combine some components, or different components, for example, the terminal device 5 may further include an input-output device, a network access device, a bus, etc.
The Processor 50 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application-Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic, discrete hardware components, etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The memory 51 may be an internal storage unit of the terminal device 5, such as a hard disk or a memory of the terminal device 5. The Memory 51 may also be an external storage device of the terminal device 5, such as a plug-in hard disk, a Smart Media Card (SM), a Secure Digital Card (SD), a Flash Memory Card (CF), and the like, which are equipped on the terminal device 5. Further, the memory 51 may also include both an internal storage unit and an external storage device of the terminal device 5. The memory 51 is used for storing the computer program and other programs and data required by the terminal device. The memory 51 may also be used to temporarily store data that has been output or is to be output.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-mentioned division of the functional units and modules is illustrated, and in practical applications, the above-mentioned function distribution may be performed by different functional units and modules according to needs, that is, the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-mentioned functions. Each functional unit and module in the embodiments may be integrated in one processing unit, or each unit may exist alone physically, or two or more units are integrated in one unit, and the integrated unit may be implemented in a form of hardware, or in a form of software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention.
In the embodiments provided in the present invention, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described system embodiments are merely illustrative, and for example, the division of the modules or units is only one logical division, and there may be other divisions when actually implemented, for example, a plurality of units or components may be combined or may be integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow of the method according to the embodiments of the present invention may also be implemented by a computer program, which may be stored in a computer-readable storage medium, and when the computer program is executed by a processor, the steps of the method embodiments may be implemented. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, and the like. It should be noted that the computer readable medium may contain content that is subject to appropriate increase or decrease as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media does not include electrical carrier signals and telecommunications signals as is required by legislation and patent practice.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not substantially depart from the spirit and scope of the embodiments of the present invention, and are intended to be included within the scope of the present invention.
Claims (8)
1. A method for writing data into a circular queue, comprising:
when any thread is ready to write data into the circular queue, judging whether other threads write data into the circular queue at present;
if other threads are writing data into the circular queue at present, returning to execute the step of judging whether other threads are writing data into the circular queue at present and the subsequent steps after the preset time length;
if no other thread writes data into the circular queue at present, each thread uses an atomic operation mode of a comparison and exchange mechanism to compete for a first pointer, and the first pointer points to the position of the next writable data in the circular queue;
writing data into the circular queue by the thread which successfully competes for the first pointer;
wherein whether other threads are currently writing data to the circular queue is determined by:
if the first pointer and the second pointer point to the same position, judging that no other thread writes data into the circular queue at present, and pointing the second pointer to the next position of the maximum position where the data can be read out from the circular queue;
if the first pointer and the second pointer do not point to the same position, judging that other threads write data into the circular queue currently;
if there is other thread writing data into the circular queue, before delaying the preset time, the method further includes:
judging whether the thread which is currently writing data into the circular queue is abnormal or not;
and if the thread which is currently writing data into the circular queue generates an exception, pushing the second pointer to the position pointed by the first pointer by using an atomic operation mode of a compare-and-swap mechanism.
2. The method of claim 1, wherein writing data to the circular queue by a thread that competes successfully for the first pointer comprises:
advancing the first pointer from a current position to a next position;
writing data to the current position by the thread which successfully competes for the first pointer;
advancing the second pointer to a location pointed to by the first pointer.
3. The method as claimed in claim 1, wherein before determining whether there are other threads currently writing data into the circular queue, the method further comprises:
initializing a count value of a counter;
whether the thread which is currently writing data into the circular queue generates an exception is judged by the following steps:
if the count value reaches a first threshold value, judging that the thread which is currently writing data into the circular queue is abnormal;
if the count value does not reach the first threshold value, judging that the thread which is currently writing data into the circular queue does not generate abnormity;
after judging whether the thread which is currently writing data into the circular queue generates an exception or not, the method further comprises the following steps:
if the thread which is currently writing data into the circular queue is not abnormal, accumulating the count value once, and then executing the step of delaying the preset time length.
4. The method for writing data into a circular queue according to claim 3, wherein before initializing the count value of the counter, the method further comprises:
recording a target position pointed by the first pointer;
if the thread currently writing data into the circular queue does not generate an exception, before accumulating the count value once, the method further includes:
judging whether the first pointer points to the target position currently;
if the first pointer points to the target position currently, the step of accumulating the count value once is executed;
if the first pointer does not point to the target position currently, returning to record the target position pointed by the first pointer;
after the atomic operation mode using the compare-and-swap mechanism advances the second pointer to the location pointed to by the first pointer, further comprising:
and if the atomic operation mode of the comparison and exchange mechanism fails to push the second pointer to the position pointed by the first pointer, returning to the step of recording the target position pointed by the first pointer.
5. A circular queue data writing apparatus, comprising:
the first judgment module is used for judging whether other threads write data into the circular queue at present or not when any thread is ready to write the data into the circular queue;
the delay module is used for returning to execute the step of judging whether other threads write data into the circular queue or not and the subsequent steps after the preset time length if other threads write data into the circular queue currently;
a competition pointer module, configured to, if no other thread is currently writing data into the circular queue, each thread uses an atomic operation mode of a compare-and-exchange mechanism to compete for a first pointer, where the first pointer points to a next position in the circular queue, where the data can be written;
the data writing module is used for writing data into the circular queue by the thread which successfully competes for the first pointer;
a first determining module, configured to determine that no other thread is currently writing data into the circular queue if the first pointer and the second pointer point to a same position, where the second pointer points to a next position of a maximum position of readable data in the circular queue;
the second judgment module is used for judging that other threads write data into the circular queue currently if the first pointer and the second pointer do not point to the same position;
the second judgment module is used for judging whether the thread which is currently writing data into the circular queue is abnormal or not;
and the pushing module is used for pushing the second pointer to the position pointed by the first pointer by using an atomic operation mode of a comparison and exchange mechanism if the thread which is currently writing data into the circular queue generates an exception.
6. The circular queue data write apparatus according to claim 5, wherein the data write module comprises:
a first pointer advancing unit for advancing the first pointer from a current position to a next position;
the data writing unit is used for writing data to the current position by a thread which successfully competes for the first pointer;
and the second pointer advancing unit is used for advancing the second pointer to the position pointed by the first pointer.
7. Terminal device comprising a memory, a processor and a computer program stored in said memory and executable on said processor, characterized in that said processor, when executing said computer program, carries out the steps of the method for writing data to a circular queue according to any one of claims 1 to 4.
8. A computer-readable storage medium, in which a computer program is stored, which, when being executed by a processor, carries out the steps of the method for writing data of a circular queue according to any one of claims 1 to 4.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810358392.7A CN108710531B (en) | 2018-04-20 | 2018-04-20 | Data writing method and device of circular queue, terminal equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810358392.7A CN108710531B (en) | 2018-04-20 | 2018-04-20 | Data writing method and device of circular queue, terminal equipment and storage medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN108710531A CN108710531A (en) | 2018-10-26 |
CN108710531B true CN108710531B (en) | 2021-06-15 |
Family
ID=63866765
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201810358392.7A Active CN108710531B (en) | 2018-04-20 | 2018-04-20 | Data writing method and device of circular queue, terminal equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108710531B (en) |
Families Citing this family (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109815029B (en) * | 2019-01-10 | 2023-03-28 | 西北工业大学 | Method for realizing communication between partitions of embedded partition operating system |
CN110750531B (en) * | 2019-09-16 | 2023-09-08 | 深圳市元征科技股份有限公司 | Data processing method and related equipment |
CN112306827B (en) * | 2020-03-25 | 2024-10-18 | 北京京东拓先科技有限公司 | Log acquisition device, method and computer readable storage medium |
CN111443873A (en) * | 2020-03-27 | 2020-07-24 | 深圳天岳创新科技有限公司 | Method and device for managing Nand Flash memory |
CN111538589B (en) * | 2020-04-14 | 2023-05-05 | 明见(厦门)技术有限公司 | Operation optimization method, device, terminal and medium for embedded OS thread blocking queue |
CN111858095B (en) * | 2020-07-17 | 2022-06-10 | 山东云海国创云计算装备产业创新中心有限公司 | Hardware queue multithreading sharing method, device, equipment and storage medium |
CN112765223B (en) * | 2021-01-26 | 2023-07-14 | 红云红河烟草(集团)有限责任公司 | Equipment parameter data alignment method based on queue |
CN113377509A (en) * | 2021-06-08 | 2021-09-10 | 上海哔哩哔哩科技有限公司 | Data processing method and system |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101753439A (en) * | 2009-12-18 | 2010-06-23 | 深圳市融创天下科技发展有限公司 | Method for distributing and transmitting streaming media |
CN103197958A (en) * | 2013-04-01 | 2013-07-10 | 天脉聚源(北京)传媒科技有限公司 | Data transmission method and system and transponder device |
CN103475899A (en) * | 2013-07-30 | 2013-12-25 | 深圳市融创天下科技股份有限公司 | Data distribution method and device |
Family Cites Families (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
WO2008155827A1 (en) * | 2007-06-20 | 2008-12-24 | Fujitsu Limited | Cache control device and control method |
US9009369B2 (en) * | 2011-10-27 | 2015-04-14 | Apple Inc. | Lookahead scheme for prioritized reads |
US10002031B2 (en) * | 2013-05-08 | 2018-06-19 | Nvidia Corporation | Low overhead thread synchronization using hardware-accelerated bounded circular queues |
CN106648461A (en) * | 2016-11-15 | 2017-05-10 | 努比亚技术有限公司 | Memory management device and method |
-
2018
- 2018-04-20 CN CN201810358392.7A patent/CN108710531B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101753439A (en) * | 2009-12-18 | 2010-06-23 | 深圳市融创天下科技发展有限公司 | Method for distributing and transmitting streaming media |
CN103197958A (en) * | 2013-04-01 | 2013-07-10 | 天脉聚源(北京)传媒科技有限公司 | Data transmission method and system and transponder device |
CN103475899A (en) * | 2013-07-30 | 2013-12-25 | 深圳市融创天下科技股份有限公司 | Data distribution method and device |
Non-Patent Citations (2)
Title |
---|
《 战术信息传输系统的研究》;余新胜;《中国优秀硕士学位论文全文数据库(信息科技辑)电子期刊》;20120215;第2012年卷(第2期);全文 * |
《Predictable Interrupt Management and Scheduling in the Composite Component-Based System》;Gabriel Parmer et al.;《 2008 Real-Time Systems Symposium》;20081208;全文 * |
Also Published As
Publication number | Publication date |
---|---|
CN108710531A (en) | 2018-10-26 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN108710531B (en) | Data writing method and device of circular queue, terminal equipment and storage medium | |
US20150186068A1 (en) | Command queuing using linked list queues | |
US9128615B2 (en) | Storage systems that create snapshot queues | |
EP2417528B1 (en) | Command and interrupt grouping for a data storage device | |
CN109947362B (en) | Managing flash memory read operations | |
US10782915B2 (en) | Device controller that schedules memory access to a host memory, and storage device including the same | |
US10325219B2 (en) | Parallel retrieval of training data from multiple producers for machine learning systems | |
CN108632624B (en) | Image data processing method and device, terminal equipment and readable storage medium | |
CN111158939A (en) | Data processing method, data processing device, storage medium and electronic equipment | |
JP2020194523A (en) | Method, apparatus, device, and storage medium for processing access request | |
CN108304272B (en) | Data IO request processing method and device | |
US7266650B2 (en) | Method, apparatus, and computer program product for implementing enhanced circular queue using loop counts | |
CN110851073B (en) | Storage device and execution method of macro instruction | |
CN117076353B (en) | Descriptor configuration method and descriptor configuration device | |
CN117834556A (en) | Multi-queue organization and scheduling method, system, storage medium and electronic equipment | |
US20200301732A1 (en) | Information processing system and non-transitory computer readable medium storing program | |
CN110399219B (en) | Memory access method, DMC and storage medium | |
CN112840309B (en) | Command scheduling method, device and storage medium | |
CN110275882A (en) | Method for writing data, system, device and readable storage medium storing program for executing based on HBASE | |
CN110007853B (en) | Nandflash command processing method, Nandflash command processing device, terminal and storage medium | |
US20160378572A1 (en) | Optimizing the initialization of a queue via a batch operation | |
CN115686789A (en) | Discrete event parallel processing method, terminal equipment and storage medium | |
CN111261206B (en) | Read-write method and device, electronic equipment and readable storage medium | |
CN112817534B (en) | Method, device, computer equipment and storage medium for improving SSD read-write performance | |
CN109885442B (en) | Performance analysis method, device, equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |