[go: up one dir, main page]
More Web Proxy on the site http://driver.im/

CN106354572A - Multi-thread data transmission method - Google Patents

Multi-thread data transmission method Download PDF

Info

Publication number
CN106354572A
CN106354572A CN201610761793.8A CN201610761793A CN106354572A CN 106354572 A CN106354572 A CN 106354572A CN 201610761793 A CN201610761793 A CN 201610761793A CN 106354572 A CN106354572 A CN 106354572A
Authority
CN
China
Prior art keywords
data
thread
read
write
value
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.)
Withdrawn
Application number
CN201610761793.8A
Other languages
Chinese (zh)
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.)
CHENGDU COLASOFT Co Ltd
Original Assignee
CHENGDU COLASOFT 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 CHENGDU COLASOFT Co Ltd filed Critical CHENGDU COLASOFT Co Ltd
Priority to CN201610761793.8A priority Critical patent/CN106354572A/en
Publication of CN106354572A publication Critical patent/CN106354572A/en
Withdrawn legal-status Critical Current

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/54Interprogram communication
    • G06F9/544Buffers; Shared memory; Pipes

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computer And Data Communications (AREA)

Abstract

The invention provides a multi-thread data transmission method. Two integer values including write Pos and read Pos are defined, wherein the write Pos refers to the current position of a data writing thread for writing data into an array space, and the read Pos refers to the current position of a data reading thread for reading the data from the array space. The write Pos value is automatically increased when the data are successfully written, and the read Pos value is automatically increased when the data are successfully read. An integer mask Pos Mask is defined, and the value of the integer mask Pos Mask is N-1. The position of the actual array space for currently writing and reading the data is determined by AND operation of the value of the Pos Mask and values of the read-write positions, namely, the current writing position is data PtrArray [write Pos &Pos Mask], and the reading position is data PtrArray [read Pos &Pos Mask]. Data transmission between two threads can be realized, namely, a queue container as a critical resource is accessed in an unlocked manner, and performance is greatly improved.

Description

A kind of multi-thread data transmission method
Technical field
The present invention relates to a kind of multi-thread data transmission method, more particularly to one kind is be applied to multi-threading correspondence, many The multi-thread data transmission method of thread accesses.
Background technology
Typically in multi-threading correspondence, can there are some visits to critical resource (multiple threads all may be accessed) simultaneously Ask, because critical resource is public, if read and write by multiple threads, critical resource can become a kind of unknown shape simultaneously State, is unsafe, and program can be led to produce baffled mistake, so program often passes through thread when using critical resource Lock, to ensure that the access of critical resource is safely controllable, ensures have any time to only have one thread of meeting to grasp by thread lock Make critical resource;But this mechanism can lead to other thread process waiting states, thus reduce system process performance.
Content of the invention
The technical problem to be solved in the present invention is to provide a kind of multi-thread data transmission method, can solve the problem that between two threads Data transfer, realizes the access of no lock, performance is greatly improved to this critical resource of queue container.
The technical solution used in the present invention is as follows: a kind of multi-thread data transmission method, method particularly includes:
During initialization system, apply for a contiguous memory space that can accommodate n pointer;Described n=2m, m is more than or equal to 1 Integer;Each pointer position is a unit, and n pointer position forms an array space dataptrarray;Described finger Pin points to user data address;
Define two integer value: writepos and readpos;Wherein, writepos represents that data write thread to be currently ready writing Enter the position in described array space;Readpos represents that digital independent thread is current and to be ready reading data from described array space Position;Data writes successfully, then writepos value is from increasing;Digital independent success, then the increasing certainly of readpos value, will be successfully read The pointer value of array locus reset, after clearing, write thread finds that this position is zero again, can be normally written new User pointer data;
When but dataptrarray writes full data do not walked by timely reading, failure can be write;When in dataptrarray When all values are all zero, failure can be read;
Define an integer mask posmask, its value is n-1;Done and computing by the value of the value of posmask and read and write position, To determine the position in the current real array space being about to and writing and read, that is, presently written position should be Dataptrarray [writepos&posmask], the position of reading is dataptrarray [readpos&posmask].
Declare into continuous memory array, the subunit sequence value that can quickly pass through array quickly reads and write number According to pointer;But when data writes and reads array afterbody, gesture is desired by the judgement of program, and this judges multi-thread Cheng Zhonghui causes performance cost, Gu present invention uses one kind can no performance cost, and data reading and write data After tail data, leap to data header;Done and computing by the value of the value of posmask and read and write position, to determine current The real position data pointer in data set space to be ready writing and read reads in dataptrarray and achieves tail head phase Connect, and no any unlocking, there is high performance.
Methods described also includes: during write data, using the lock bussing technique of intel, in the way of atom, judges simultaneously Whether writing position is empty, if it is empty, then writes, and if not empty then it represents that queue has been write full, now write thread is absorbed in Wait;When reading data, using the lock bussing technique of intel, in an atomic manner judge whether reading position is empty, if not Sky, then read and return, and reading position is reset, if it is empty then it represents that whole queue has all been read, now read line Journey is absorbed in wait;Reading thread-data is notified to can read when one data pointer of successful write, now, if reading thread For resting state, then will be waken up;Notify write thread-data writable when one data pointer of successful reading, now, If write thread is resting state, will be waken up.
Because reading and write thread being asynchronous, readpos and writepos there may be the situation of overlap (i.e. When data space be fully written or be space-time), now read and write be same position, be a critical zone, this feelings Condition must make data be protected, and compare traditional thread lock and have very high cost, and this method is with a kind of high side of performance Formula achieves the efficient read-write of critical zone.
In described memory headroom, with every 8 bytes for a unit.Memory headroom is with 8 bytes, i.e. a uint64_t For a unit, the value of the sizeof (void*) under any system can be deposited, that is, store a value (pointer of a pointer Up to one uint64_t).
Compared with prior art, the invention has the beneficial effects as follows: can solve the problem that data transfer between two threads, that is, to queue This critical resource of container realizes the access of no lock, and performance is greatly improved;Achieve critical in the way of a kind of performance is high The efficient read-write in area.
Specific embodiment
In order that the objects, technical solutions and advantages of the present invention become more apparent, with reference to embodiments, to the present invention It is further elaborated.It should be appreciated that specific embodiment described herein, only in order to explain the present invention, is not used to limit Determine the present invention.
Any feature disclosed in this specification (including summary), unless specifically stated otherwise, all can be by other equivalent or tools The alternative features having similar purpose are replaced.I.e., unless specifically stated otherwise, each feature is a series of equivalent or similar characteristics One of example.
Specific embodiment 1
A kind of multi-thread data transmission method, method particularly includes:
During initialization system, apply for a contiguous memory space that can accommodate n pointer;Described n=2m, m is more than or equal to 1 Integer;Each pointer position is a unit, and n pointer position forms an array space dataptrarray;Described finger Pin points to user data address;
Define two integer value: writepos and readpos;Wherein, writepos represents that data write thread to be currently ready writing Enter the position in described array space;Readpos represents that digital independent thread is current and to be ready reading data from described array space Position;Data writes successfully, then writepos value is from increasing;Digital independent success, then the increasing certainly of readpos value, will be successfully read The pointer value of array locus reset, after clearing, write thread finds that this position is zero again, can be normally written new User pointer data;
When but dataptrarray writes full data do not walked by timely reading, failure can be write;When in dataptrarray When all values are all zero, failure can be read;
Define an integer mask posmask, its value is n-1;Done and computing by the value of the value of posmask and read and write position, To determine the position in the current real array space being about to and writing and read, that is, presently written position should be Dataptrarray [writepos&posmask], the position of reading is dataptrarray [readpos&posmask].
Specific embodiment 2
On the basis of specific embodiment 1, methods described also includes: during write data, using the lock bussing technique of intel, with The mode of atom, judges whether writing position is empty simultaneously, if it is empty, then writes, if not empty then it represents that queue has been write completely , now write thread be absorbed in wait:
Void* dataptr is a data pointer needing to be written into queue,
do{
uint32_t writelocation = _sync_fetch_and_add(&writepos,1);
uint64_t* writeptr = (uint64_t*)dataptrarray[posmask&writelocation];
if(__sync_bool_compare_and_swap(writeptr,0,(uint64_t)dataptr))
{
notifyread();
return true;// write successfully
}
else
{
wait();
}
}
When reading data, using the lock bussing technique of intel, in an atomic manner judge whether reading position is empty, if not Sky, then read and return, and reading position is reset, if it is empty then it represents that whole queue has all been read, now read line Journey is absorbed in wait:
Uint64_t readptrvalue=0, is the value reading from queue, is returned to user,
uint32_t readlocaltion = __sync_fetch_and_add(&readpos,1);
uint64_t* readptr = (uint64_t*)dataptrarray[posmask&readlocaltion];
if((readptrvalue =__sync_lock_test_and_set(readptr,0)) != 0 )
{
notifywrite();
return readptrvalue;// read successfully
}
else
{
wait();
}
Reading thread-data is notified to can read when one data pointer of successful write, now, if reading thread is dormancy State, then will be waken up;Notify write thread-data writable when one data pointer of successful reading, now, if write Entering thread is resting state, then will be waken up.
Specific embodiment 3
On the basis of specific embodiment 1 or 2, in described memory headroom, with every 8 bytes for a unit.Memory headroom is with 8 Individual byte, that is, a uint64_t is a unit, can deposit the value of the sizeof (void*) under any system, that is, store The value (up to one uint64_t of a pointer) of one pointer.

Claims (3)

1. a kind of multi-thread data transmission method, method particularly includes:
During initialization system, apply for a contiguous memory space that can accommodate n pointer;Described n=2m, m is more than or equal to 1 Integer;Each pointer position is a unit, and n pointer position forms an array space dataptrarray;Described pointer Point to user data address;
Define two integer value: writepos and readpos;Wherein, writepos represents that data write thread to be currently ready writing Enter the position in described array space;Readpos represents that digital independent thread is current and to be ready reading data from described array space Position;Data writes successfully, then writepos value is from increasing;Digital independent success, then the increasing certainly of readpos value, will be successfully read Array locus pointer value reset;
Define an integer mask posmask, its value is n-1;Done and computing by the value of the value of posmask and read and write position, To determine the position in the current real array space being about to and writing and read, that is, presently written position should be Dataptrarray [writepos&posmask], the position of reading is dataptrarray [readpos&posmask].
2. multi-thread data transmission method according to claim 1, methods described also includes: during write data, adopts The lock bussing technique of intel, in the way of atom, judges whether writing position is empty simultaneously, if it is empty, then writes, if not Empty then it represents that queue has been write full, now write thread is absorbed in wait;When reading data, using the lock bus skill of intel Art, in an atomic manner judge that whether reading position is empty, if not empty, then read and return, and reading position is reset, if For empty then it represents that whole queue has all been read, now read thread and be absorbed in wait;Write a data pointer when successful Shi Tongzhi reads thread-data and can read;Notify write thread-data writable when one data pointer of successful reading.
3. multi-thread data transmission method according to claim 1 and 2, in described memory headroom, with every 8 bytes for one Individual unit.
CN201610761793.8A 2016-08-31 2016-08-31 Multi-thread data transmission method Withdrawn CN106354572A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610761793.8A CN106354572A (en) 2016-08-31 2016-08-31 Multi-thread data transmission method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610761793.8A CN106354572A (en) 2016-08-31 2016-08-31 Multi-thread data transmission method

Publications (1)

Publication Number Publication Date
CN106354572A true CN106354572A (en) 2017-01-25

Family

ID=57856506

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610761793.8A Withdrawn CN106354572A (en) 2016-08-31 2016-08-31 Multi-thread data transmission method

Country Status (1)

Country Link
CN (1) CN106354572A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113778674A (en) * 2021-08-31 2021-12-10 上海弘积信息科技有限公司 Lock-free implementation method of load balancing equipment configuration management under multi-core

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101631139A (en) * 2009-05-19 2010-01-20 华耀环宇科技(北京)有限公司 Load balancing software architecture based on multi-core platform and method therefor
CN103488717A (en) * 2013-09-11 2014-01-01 北京华胜天成科技股份有限公司 Lock-free data gathering method and lock-free data gathering device
CN103713884A (en) * 2013-12-18 2014-04-09 珠海金山网络游戏科技有限公司 Multithreading method and system for processing data through array and multithreading processor
CN104123177A (en) * 2014-07-18 2014-10-29 无锡梵天信息技术股份有限公司 Lockless multithreading data synchronization method

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101631139A (en) * 2009-05-19 2010-01-20 华耀环宇科技(北京)有限公司 Load balancing software architecture based on multi-core platform and method therefor
CN103488717A (en) * 2013-09-11 2014-01-01 北京华胜天成科技股份有限公司 Lock-free data gathering method and lock-free data gathering device
CN103713884A (en) * 2013-12-18 2014-04-09 珠海金山网络游戏科技有限公司 Multithreading method and system for processing data through array and multithreading processor
CN104123177A (en) * 2014-07-18 2014-10-29 无锡梵天信息技术股份有限公司 Lockless multithreading data synchronization method

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113778674A (en) * 2021-08-31 2021-12-10 上海弘积信息科技有限公司 Lock-free implementation method of load balancing equipment configuration management under multi-core

Similar Documents

Publication Publication Date Title
Pelley et al. Memory persistency
EP2223217B1 (en) System, apparatus, and method for modifying the order of memory accesses
CN101410797B (en) Method, device and system for transactional memory in out-of-order processors
TWI494852B (en) Processor and method for maintaining an order of instructions in relation to barriers in the processor
EP3270290B1 (en) Ddr memory error recovery
CN101059783B (en) Transactional memory virtualization
DE69627950T2 (en) Fifo buffer with blocked exchange
US9081606B2 (en) Asynchronous persistent stores for transactions
EP2641171B1 (en) Preventing unintended loss of transactional data in hardware transactional memory systems
JP2010524133A5 (en)
US20080288691A1 (en) Method and apparatus of lock transactions processing in single or multi-core processor
US8954644B2 (en) Apparatus and method for controlling memory
US11941429B2 (en) Persistent multi-word compare-and-swap
US20140156959A1 (en) Concurrent array-based queue
US6941308B1 (en) Methods and apparatus for accessing a doubly linked list in a data storage system
CN106354572A (en) Multi-thread data transmission method
WO2016106738A1 (en) Transaction conflict detection method and apparatus and computer system
US20070050527A1 (en) Synchronization method for a multi-processor system and the apparatus thereof
US8452920B1 (en) System and method for controlling a dynamic random access memory
US20220405214A1 (en) Memory controller with hybrid dram/persistent memory channel arbitration
CN117407148B (en) Data writing method, data reading device, electronic equipment and storage medium
CN118963991A (en) Data writing method, data reading device, electronic equipment and storage medium
Schwaderer et al. OCP Signal Groups and Phases

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
WW01 Invention patent application withdrawn after publication

Application publication date: 20170125

WW01 Invention patent application withdrawn after publication