CN111143079B - Multi-read multi-write lock-free queue implementation method - Google Patents

Multi-read multi-write lock-free queue implementation method Download PDF

Info

Publication number
CN111143079B
CN111143079B CN201911343744.2A CN201911343744A CN111143079B CN 111143079 B CN111143079 B CN 111143079B CN 201911343744 A CN201911343744 A CN 201911343744A CN 111143079 B CN111143079 B CN 111143079B
Authority
CN
China
Prior art keywords
read
write
queue
lock
free
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
Application number
CN201911343744.2A
Other languages
Chinese (zh)
Other versions
CN111143079A (en
Inventor
李朝铭
王建华
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Inspur Software Co Ltd
Original Assignee
Inspur Software Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Inspur Software Co Ltd filed Critical Inspur Software Co Ltd
Priority to CN201911343744.2A priority Critical patent/CN111143079B/en
Publication of CN111143079A publication Critical patent/CN111143079A/en
Application granted granted Critical
Publication of CN111143079B publication Critical patent/CN111143079B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements 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/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements 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/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues

Abstract

The invention provides a multi-read multi-write lock-free queue implementation method, which belongs to the technical field of data processing, and comprises the steps of firstly defining a set of read-write lock-free queue structure, a queue write API and a read API, wherein the read-write lock-free queue is specifically implemented by a circular chain table; then, according to the maximum concurrency and the maximum value N in the multi-read multi-write, creating N relatively independent read-write lock-free queues, and defining a structure pointer array formed by N elements, wherein the structure pointer array points to node addresses in one read-write lock-free queue respectively; and finally, defining a multi-read multi-write queue write API and a read API according to the pointer array, so that the requirement of concurrent reading and writing without lock in a multithreading environment can be met.

Description

Multi-read multi-write lock-free queue implementation method
Technical Field
The invention relates to a data processing technology, in particular to a method for realizing a multi-read multi-write lock-free queue.
Background
Queues are a commonly used data structure for service decoupling, asynchronous processing to improve performance, etc., and the use of a queue is generally divided into producer and consumer, one queue may have one producer and one consumer, abbreviated as read-write, and may have multiple producers and multiple consumers, abbreviated as multiple read-write. Whether in a read-write mode or a multi-read-write mode, concurrent read-write problems of the same message of the queue can be faced in a multi-thread environment, and the concurrent read-write can cause data disorder, so that the queue cannot work correctly.
To solve the concurrent read and write problem, locks are typically introduced, such as pthread's mutex locks or system lower level atomic locks. The introduction of the lock solves the concurrent read-write problem, but a new problem, namely performance reduction problem, is generated, and the more concurrent operations on the queue, the more performance reduction is, so that the queue cannot meet the performance requirement. For this reason, a concept of a read-write lock-free queue has been introduced, i.e. the concurrent operation of the queue does not use locks, which is very high performing but only for a read-write concurrency mode. People continue to reform and put forward a multi-read-one-write, a multi-read-many-write mode or a multi-read-many-write mode, and some implementations actually use low-level atomic locks, which have higher mutual exclusion performance than pthread, but still have concurrent performance problems, are not true lock-free queues, and some implementations do not achieve true concurrency, and have data error problems during concurrency.
Disclosure of Invention
In order to solve the technical problems, the invention provides a multi-read multi-write lock-free queue implementation method, which is used for logically forming a large lock-free queue from a plurality of read-write lock-free queues on the basis of successfully implementing the read-write lock-free queues and supporting multi-read multi-write lock-free concurrent operation.
The invention relates to a method for realizing a multi-read multi-write lock-free queue, which logically forms a large lock-free queue from a plurality of read-write lock-free queues on the basis of successfully realizing the read-write lock-free queue and supports multi-read multi-write lock-free concurrent operation.
The method is a real lock-free operation, supports multi-read multi-write lock-free concurrent high-performance operation, and can meet the requirement of multi-thread concurrent processing.
The technical scheme of the invention is as follows:
a method for realizing a multi-read multi-write lock-free queue,
firstly defining a set of read-write lock-free queue structure, a queue write API and a read API, wherein the read-write lock-free queue is specifically realized by a circular chain table; then, according to the maximum concurrency and the maximum value N in the multi-read multi-write, creating N relatively independent read-write lock-free queues, and defining a structure pointer array formed by N elements, wherein the structure pointer array points to node addresses in one read-write lock-free queue respectively; and finally defining a multi-read multi-write queue write API and a read API according to the pointer array.
Further, the method comprises the steps of,
the maximum concurrency N can be set, the maximum number of messages which can be contained in each read-write lock-free queue can also be set, and the multi-read multi-write lock-free queue is generated according to the two parameters set by a user.
Further, the method comprises the steps of,
the parameters of the write API of the multi-read multi-write queue comprise four parameters of an Id number of a write thread, an Id of a message, an address of the message and a size of the message, and the parameters can be expanded according to the requirement, and the Id number of each write thread is unique.
The multi-read multi-write queue write API uniquely selects a read-write lock-free queue according to the Id number of the thread, writes the message into the lock-free queue and returns a write result, and the write thread and the read-write lock-free queue are in one-to-one correspondence.
Further, the method comprises the steps of,
the parameters of the read API of the multi-read multi-write queue comprise four parameters of read thread Id number, message Id address, message address and message size address, and can be expanded according to the requirement, and the Id number of each read thread is unique.
The read API of the multi-read multi-write queue uniquely selects a read-write lock-free queue according to the Id number of the thread, reads a message from the lock-free queue and returns a read result, and the read thread and the read-write lock-free queue are in one-to-one correspondence.
Further, the method comprises the steps of,
when the number of read threads is smaller than the number of write threads, some of the read threads will take messages from a plurality of read-write lock-free queues, but one read-write lock-free queue will only be allocated to one read thread.
The invention has the beneficial effects that
The method can meet the requirement of high-performance concurrency operation on the same queue in a multithreading environment, and has higher concurrency performance compared with a queue with a lock.
Drawings
Fig. 1 is a schematic of the workflow of the present invention.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are some embodiments of the present invention, but not all embodiments, and all other embodiments obtained by those skilled in the art without making any inventive effort based on the embodiments of the present invention are within the scope of protection of the present invention.
The invention relates to a multi-read multi-write lock-free queue implementation method, which comprises the steps of defining a set of read-write lock-free queue structure, a queue write API and a read API (the realization of the read-write lock-free queue is not innovation of the patent but is only a component part of the scheme of the patent), wherein the read-write lock-free queue is specifically realized by a circular linked list; then, according to the maximum concurrency and the maximum value N in the multi-read multi-write, creating N relatively independent read-write lock-free queues, and defining a structure pointer array formed by N elements, wherein the structure pointer array points to node addresses in one read-write lock-free queue respectively; and finally, defining a multi-read multi-write queue write API and a read API according to the structure pointer array, so that the requirement of concurrent reading and writing without locks in a multithreading environment can be met.
The maximum concurrency N can be set, and the maximum number of messages which can be contained in each read-write lock-free queue can also be set.
The parameters of the multi-read multi-write queue write API comprise four parameters of a write thread Id number, a message Id, a message address and a message size, the parameters can be expanded according to the need, the Id number of each write thread is unique, the multi-read multi-write queue write API uniquely selects a read-write lock-free queue according to the Id number of the thread, the message is written into the lock-free queue, and a writing result is returned, and the write thread and the read-write lock-free queue are in one-to-one correspondence.
The parameters of the multi-read multi-write queue read API comprise four parameters of read thread Id number, message Id address, message address and message size address, and can be expanded according to the need, the Id number of each read thread is unique, the multi-read multi-write queue read API uniquely selects a read-write lock-free queue according to the Id number of the thread, one message is read from the lock-free queue, the read result is returned, and the read thread and the read-write lock-free queue are in one-to-one correspondence.
When the number of read threads is smaller than the number of write threads, some of the read threads will take messages from a plurality of read-write lock-free queues, but one read-write lock-free queue will only be allocated to one read thread.
Through the scheme, the original multi-read multi-write concurrency operation on the same queue is internally converted into a plurality of read-one-write lock-free parallel operations, so that the effect of the multi-read multi-write lock-free queue is realized, and the multi-read multi-write lock-free queue has higher concurrency performance compared with a lock queue.
The foregoing description is only illustrative of the preferred embodiments of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims (2)

1. A method for realizing a multi-read multi-write lock-free queue is characterized in that,
firstly defining a set of read-write lock-free queue structure, a queue write API and a read API, wherein the read-write lock-free queue is realized by a circular chain table; then, according to the maximum concurrency and the maximum value N in the multi-read multi-write, creating N relatively independent read-write lock-free queues, and defining a structure pointer array formed by N elements, wherein the structure pointer array points to node addresses in one read-write lock-free queue respectively; finally defining a multi-read multi-write queue write API and a read API according to the pointer array;
the maximum concurrency N can be set, the maximum number of messages which can be contained in each read-write lock-free queue can also be set, and a multi-read multi-write lock-free queue is generated according to the two parameters set by a user;
the parameters of the write API of the multi-read multi-write queue comprise four parameters of a write thread Id number, a message Id, a message address and a message size, and can be expanded as required;
the Id number of each writing thread is unique, the multi-read multi-write queue writing API uniquely selects a read-write lock-free queue according to the Id number of the thread, and writes the message into the lock-free queue and returns a writing result, and the writing thread and the read-write lock-free queue are in one-to-one correspondence;
the parameters of the read API of the multi-read multi-write queue comprise four parameters of read thread Id number, message Id address, message address and message size address, and can be expanded according to the requirement;
the Id number of each read thread is unique, the read API of the multi-read multi-write queue uniquely selects a read-write lock-free queue according to the Id number of the thread, reads a message from the lock-free queue and returns a read result, and the read thread and the read-write lock-free queue are in one-to-one correspondence.
2. The method of claim 1, wherein the step of determining the position of the substrate comprises,
when the number of read threads is smaller than the number of write threads, the read threads need to fetch the information from more than one read-write lock-free queue, but one read-write lock-free queue is only allocated to one read thread.
CN201911343744.2A 2019-12-24 2019-12-24 Multi-read multi-write lock-free queue implementation method Active CN111143079B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911343744.2A CN111143079B (en) 2019-12-24 2019-12-24 Multi-read multi-write lock-free queue implementation method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911343744.2A CN111143079B (en) 2019-12-24 2019-12-24 Multi-read multi-write lock-free queue implementation method

Publications (2)

Publication Number Publication Date
CN111143079A CN111143079A (en) 2020-05-12
CN111143079B true CN111143079B (en) 2024-04-16

Family

ID=70519572

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911343744.2A Active CN111143079B (en) 2019-12-24 2019-12-24 Multi-read multi-write lock-free queue implementation method

Country Status (1)

Country Link
CN (1) CN111143079B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111767281A (en) * 2020-05-15 2020-10-13 北京奇艺世纪科技有限公司 Data processing method and device, electronic equipment and storage medium
CN113176896B (en) * 2021-03-19 2022-12-13 中盈优创资讯科技有限公司 Method for randomly taking out object based on single-in single-out lock-free queue

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104484128A (en) * 2014-11-27 2015-04-01 盛科网络(苏州)有限公司 Read-once and write-once storage based read-more and write more storage and implementation method thereof
CN104639596A (en) * 2013-11-08 2015-05-20 塔塔咨询服务有限公司 System and method for multiple sender support in low latency fifo messaging using rdma
CN106293973A (en) * 2016-08-17 2017-01-04 深圳市金证科技股份有限公司 Lock-free message queue communication means and system
CN110011936A (en) * 2019-03-15 2019-07-12 北京星网锐捷网络技术有限公司 Thread scheduling method and device based on multi-core processor

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8656409B2 (en) * 2005-12-29 2014-02-18 Intel Corporation High performance queue implementations in multiprocessor systems
US7962923B2 (en) * 2005-12-30 2011-06-14 Level 3 Communications, Llc System and method for generating a lock-free dual queue
TW201007557A (en) * 2008-08-06 2010-02-16 Inventec Corp Method for reading/writing data in a multithread system

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104639596A (en) * 2013-11-08 2015-05-20 塔塔咨询服务有限公司 System and method for multiple sender support in low latency fifo messaging using rdma
CN104484128A (en) * 2014-11-27 2015-04-01 盛科网络(苏州)有限公司 Read-once and write-once storage based read-more and write more storage and implementation method thereof
CN106293973A (en) * 2016-08-17 2017-01-04 深圳市金证科技股份有限公司 Lock-free message queue communication means and system
CN110011936A (en) * 2019-03-15 2019-07-12 北京星网锐捷网络技术有限公司 Thread scheduling method and device based on multi-core processor

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
王义涛 ; 张莉 ; .一种多线程同步防死锁算法研究.计算机与数字工程.2007,(第08期),全文. *

Also Published As

Publication number Publication date
CN111143079A (en) 2020-05-12

Similar Documents

Publication Publication Date Title
KR101203297B1 (en) Direct update software transactional memory
US8095727B2 (en) Multi-reader, multi-writer lock-free ring buffer
CN107368362B (en) Multithreading/multiprocessing lock-free processing method and system for disk read-write data
CN111143079B (en) Multi-read multi-write lock-free queue implementation method
US20130227194A1 (en) Active non-volatile memory post-processing
US8312196B2 (en) Dual processor system and method for using the same
CN103413569B (en) One reads and one writes static RAM
JP2004054933A (en) Deferment method and device for memory allocation
TW201248503A (en) Detecting potential access errors in a multi-threaded application
US8006064B2 (en) Lock-free vector utilizing a resource allocator for assigning memory exclusively to a thread
US8972629B2 (en) Low-contention update buffer queuing for large systems
CN112905365B (en) Data processing method, device, equipment and medium
US10824424B2 (en) Efficient lock-free multi-word compare-and-swap
US8972693B2 (en) Hardware managed allocation and deallocation evaluation circuit
CN112764679B (en) Dynamic capacity expansion method and terminal
WO2020118713A1 (en) Bit width matching circuit, data writing apparatus, data reading apparatus, and electronic device
US8615639B2 (en) Lock-free release of shadow pages in a data storage application
US20140115215A1 (en) Tiered locking of resources
CN109726023B (en) Graphic task synchronous management method supporting multithreading and multiple contexts
CN110874273B (en) Data processing method and device
US20210382654A1 (en) WRITE ORDERING IN SSDs
US6823467B1 (en) Method and apparatus for arbitrary resolution interval timeouts
US7035908B1 (en) Method for multiprocessor communication within a shared memory architecture
CN111274161A (en) Location-aware memory with variable latency for accelerated serialization algorithms
CN110059115A (en) A kind of method for reading data and device

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
CB02 Change of applicant information
CB02 Change of applicant information

Country or region after: China

Address after: 271000 Langchao science and Technology Park, 527 Dongyue street, Tai'an City, Shandong Province

Applicant after: INSPUR SOFTWARE Co.,Ltd.

Address before: No. 1036, Shandong high tech Zone wave road, Ji'nan, Shandong

Applicant before: INSPUR SOFTWARE Co.,Ltd.

Country or region before: China

GR01 Patent grant
GR01 Patent grant