CN113010325A - Method and device for realizing read-write lock and electronic equipment - Google Patents

Method and device for realizing read-write lock and electronic equipment Download PDF

Info

Publication number
CN113010325A
CN113010325A CN202110280807.5A CN202110280807A CN113010325A CN 113010325 A CN113010325 A CN 113010325A CN 202110280807 A CN202110280807 A CN 202110280807A CN 113010325 A CN113010325 A CN 113010325A
Authority
CN
China
Prior art keywords
lock
write
read
count
locking
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.)
Granted
Application number
CN202110280807.5A
Other languages
Chinese (zh)
Other versions
CN113010325B (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.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology 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 Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN202110280807.5A priority Critical patent/CN113010325B/en
Publication of CN113010325A publication Critical patent/CN113010325A/en
Application granted granted Critical
Publication of CN113010325B publication Critical patent/CN113010325B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

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
    • G06F9/524Deadlock detection or avoidance
    • 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
    • G06F9/526Mutual exclusion algorithms
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

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)

Abstract

The disclosure provides a method and a device for realizing a read-write lock and electronic equipment, and relates to the technical fields of mutual exclusion locks, network communication and the like in computer technology. The specific scheme is as follows: under the condition that a first write request is received, locking a write count lock, and increasing the write count by one, wherein the initialization value of the write count is zero; locking the read lock under the condition that the write count is one; unlocking the write count lock under the condition that the read lock is in a locked state; and under the condition that the write count lock is in an unlocked state, locking the write lock, and performing write operation on a first resource corresponding to the first write request. After receiving the first request, the read lock is locked under the condition that the write count is one, so that subsequent read requests cannot be continuously locked, if the write lock is unlocked and released, the write lock can be locked for the first write request, namely, the write lock is locked, and then the first resource corresponding to the first write request can be subjected to write operation, so that write priority is realized, and therefore, the efficiency of performing write operation can be improved.

Description

Method and device for realizing read-write lock and electronic equipment
Technical Field
The present application relates to the technical field of mutual exclusion locks and network communications in computer technologies, and in particular, to a method and an apparatus for implementing a read-write lock, and an electronic device.
Background
With the popularity of multi-core computers today, multithreading enables more efficient use of multi-core computer resources. But multithreading also presents complications in terms of data security. For example, when multiple threads simultaneously operate on a sharable resource variable, data inconsistencies and conflicts may result. A lock is one such mechanism that controls access to a resource by a thread and is used to ensure data coherency.
At present, there are many selectable locks, for example, a mutex lock, a spin lock, a read-write lock, and the like, where for the mutex lock, only one thread can access the sharable resource at the same time, that is, only one thread can read or write the shared resource at the same time. For the spin lock, the function is similar to that of a mutual exclusion lock, only the mutual exclusion lock can be dormant to enable the CPU when being blocked, the spin lock always occupies the CPU, and the spin lock waits until the lock is obtained. The read-write lock comprises a pair of related locks, namely a read lock and a write lock, wherein the read lock and the write lock are mutually exclusive, namely only reading or writing can be carried out at the same time, the read lock is used for read-only operation, and the write lock is used for writing operation. The read lock can be operated by a plurality of read threads at the same time, namely, the plurality of read threads can simultaneously read the resource, the write lock is unique, and only one thread can access the resource to perform write operation at the same time.
Disclosure of Invention
The application provides a method and a device for realizing a read-write lock and electronic equipment.
In a first aspect, an embodiment of the present application provides a method for implementing a read-write lock, where the method includes:
under the condition that a first write request is received, locking a write count lock, and increasing a write count by one, wherein the initialization value of the write count is zero;
locking the read lock when the write count is one;
when the read lock is in a locked state, unlocking the write count lock;
and under the condition that the write count lock is in an unlocked state, locking the write lock, and performing write operation on the first resource corresponding to the first write request.
In an implementation manner of the read/write lock in this embodiment, after receiving the first write request, the write count lock is locked, the write count is incremented by one, and the read lock is locked if the write count is one, and then, after the write lock is locked, the first resource corresponding to the first write request may be subjected to write operation. After receiving the first request, the read lock is locked under the condition that the write count is one, so that subsequent read requests cannot be continuously locked, if the write lock is unlocked and released, the write lock can be locked for the first write request, namely, the write lock is locked, and then the first resource corresponding to the first write request can be subjected to write operation, so that write priority is realized, and therefore, the efficiency of performing write operation can be improved.
In a second aspect, an embodiment of the present application provides an apparatus for implementing a read/write lock, where the apparatus includes:
the first locking counting module is used for locking a writing counting lock and increasing the writing counting by one under the condition of receiving a first writing request, wherein the initialization value of the writing counting is zero;
the first locking module is used for locking the read lock under the condition that the write count is one;
the first unlocking module is used for unlocking the write counting lock under the condition that the read lock is in a locked state;
and the writing module is used for locking the writing lock and performing writing operation on the first resource corresponding to the first writing request under the condition that the writing counting lock is in an unlocked state.
In a third aspect, an embodiment of the present application further provides an electronic device, including:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute a method for implementing a read-write lock provided by the embodiments of the present application.
In a fourth aspect, an embodiment of the present application further provides a non-transitory computer-readable storage medium storing computer instructions for causing a computer to execute an implementation method of a read-write lock provided by the embodiments of the present application.
In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, the computer program implements the implementation method of the read-write lock provided in the embodiments of the present application.
Drawings
The drawings are included to provide a better understanding of the present solution and are not intended to limit the present application. Wherein:
FIG. 1 is a flowchart illustrating a method for implementing a read/write lock according to an embodiment of the present disclosure;
FIG. 2 is a second flowchart illustrating a method for implementing a read/write lock according to an embodiment of the present application;
FIG. 3 is a block diagram of an apparatus for implementing a read/write lock according to an embodiment of the present disclosure;
FIG. 4 is a block diagram of an electronic device for implementing a method for implementing a read-write lock according to an embodiment of the present application.
Detailed Description
The following description of the exemplary embodiments of the present application, taken in conjunction with the accompanying drawings, includes various details of the embodiments of the application for the understanding of the same, which are to be considered exemplary only. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the present application. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
As shown in fig. 1, according to an embodiment of the present application, the present application provides a method for implementing a read-write lock, where the method includes:
step S101: in the event that a first write request is received, the write count lock is locked and the write count is incremented by one, wherein the write count has an initialized value of zero.
The write count may be initialized to zero and the read count may be initialized to zero, and additionally, the read count lock, write count lock, read lock, and write lock may be initialized. Under the condition that the first write request is received, the fact that write operation needs to be conducted on corresponding resources is indicated, data are modified, at the moment, the write count lock can be locked firstly, and therefore the write count lock is in a locked state. In the case of receiving the first write request, after locking the write count lock, it indicates that the write count may be modified, and in this embodiment, the write count may be incremented by one.
It should be noted that a plurality of first write requests may be received, and each time a first write request is received, the write count lock may be locked and the write count may be incremented by one, so that the write count may be incremented once each time a first write request is received, and the number of times of incrementing is the same as the number of first write requests.
Step S102: in the case of a write count of one, the read lock is locked.
Here, the write count is the incremented write count, and when it is one, the read lock is acquired, and when the read lock is in the unlocked state, the read lock is locked. In addition, as an example, if another write request is received again subsequently, the write count is increased by one, the write count is greater than one, and if the write count is greater than one, it indicates that the write count has passed the case where the write count is one, that is, it indicates that the read lock has been locked, and the read lock has been added for the first write request. In addition, it should be noted that the first write request is used to request a write operation to the corresponding first resource.
Step S103: and unlocking the write count lock when the read lock is in a locked state.
If the write count is one, the read lock is locked, and then the state of the read lock is the locked state, so that the write count lock can be unlocked, or if the write count is greater than one, the read lock is locked, and the write count lock can be unlocked without locking the read lock.
Step S104: and under the condition that the write count lock is in an unlocked state, locking the write lock, and performing write operation on a first resource corresponding to the first write request.
After the write count lock is unlocked, the write lock can be locked, that is, the write lock can be occupied, and after the write lock is locked, the first write request has the authority to write the first resource, that is, the first resource corresponding to the first write request can be written. Because the write lock is locked, other write requests cannot occupy the write lock to perform write operation, write mutual exclusion is realized, and consistency of the first resource is ensured.
In an implementation manner of the read/write lock in this embodiment, after receiving the first write request, the write count lock is locked, the write count is incremented by one, and the read lock is locked if the write count is one, and then, after the write lock is locked, the first resource corresponding to the first write request may be subjected to write operation. After receiving the first request, the read lock is locked under the condition that the write count is one, so that subsequent read requests cannot be continuously locked, if the write lock is unlocked and released, the write lock can be locked for the first write request, namely, the write lock is locked, and then the first resource corresponding to the first write request can be subjected to write operation, so that write priority is realized, and therefore, the efficiency of performing write operation can be improved.
In one embodiment, after performing a write operation on a first resource corresponding to a first write request, the method further includes: unlocking the write lock; locking a write counter lock; in the case that the write count is greater than zero, decrementing the write count by one; in the case where the write count is zero, the read lock is unlocked, and the write count lock is unlocked.
In the embodiment of the present application, the determination of the write count is performed on the latest write count, for example, after the write count is decremented by one, if the write count is zero, the write count in the unlocking of the read lock is the latest write count after the decrement processing. After the first resource corresponding to the first write request performs write access, the write lock can be released, so that other write requests can add the write lock, and the like. In this embodiment, the write count may be understood as a counted number of received write requests, and each time a write request is received, an operation may be performed to increase the write count by one, each time a write operation corresponding to the write request is performed, which indicates that the write request has ended, a write lock added to the write request may be released, and each time the write lock is released, a process of decreasing the write count by one may be performed.
That is, in this embodiment, a write count of zero may indicate that each write request has ended, and the read lock is unlocked only when the write count is zero, and a subsequent read requester may add the read lock to perform a read operation.
In one embodiment, after locking the write count lock, the method further comprises: and unlocking the write count lock under the condition that the write count is not zero.
The write count in this embodiment is the latest write count, and if the write count is not zero, it indicates that there is still a write request that has not ended, and the write count lock is unlocked, and at this time, the read lock does not need to be unlocked, and other read requests still cannot add a read lock. Therefore, the read-write mutual exclusion can be realized, and the read-write mutual exclusion performance is improved.
As shown in fig. 2, in an embodiment, before locking the write count lock in response to the first write request in case of receiving the first write request, the method further includes:
step S1011: locking a read lock and locking a read count lock in the event that a first read request is received;
step S1012: increasing the read count by one, wherein the read count has an initialized value of zero;
step S1013: locking the write lock if the read count is one;
step S1014: unlocking the reading counting lock under the condition that the writing lock is in a locking state;
step S1015: and under the condition that the reading counting lock is in an unlocked state, unlocking the reading lock.
That is, the first read request may have been received before the first write request was received, at which point the read lock may be locked, and the read count lock may be locked, at which point the read count may be written to, i.e., incremented by one. The read count in the embodiment of the present application may be understood as the latest read count, for example, when the read count is one after the bet count is increased by one, the read count in the lock of the write lock is the latest read count after the bet count is increased by one. If the read count is one, the write lock may be locked. It should be noted that the write lock is locked when the read count is one, and it is understood that the write lock is locked when the read count is one and the write lock is in the unlocked state. After the write lock is locked, the readable counting lock is unlocked, and after the read counting lock is unlocked, the read lock can be unlocked.
In this embodiment, after receiving the first read request, the write lock may be locked if the read count is one, so that the subsequent write request cannot be locked, and only after the write lock is unlocked, the subsequent write request may be locked to perform write operation, so as to implement the read-write mutual exclusion and improve the read-write mutual exclusion performance.
It should be noted that a plurality of first read requests may be received, and each time a first read request is received, the read count lock may be locked and the read count may be incremented by one, so that the read count may be incremented once each time a first read request is received, and the number of times of incrementing may be the same as the number of first read requests.
As an example, since the write lock may be locked before the read lock is unlocked, and the other write requests cannot be added to the write lock for performing the write operation, after the read lock is unlocked, the second resource corresponding to the first read request may be read while the write lock is locked, so as to implement the mutual exclusion between read and write.
In one embodiment, after the reading lock is unlocked, the method further comprises the following steps:
locking the counter lock;
decrementing the read count by one if the read count is greater than zero;
in the case where the read count is zero, the write lock is unlocked, and the read count lock is unlocked.
In the embodiment of the present application, the determination of the read count is performed on the latest read count, for example, after the read count is decremented by one, if the read count is zero, the read count in the unlock of the read lock is the latest read count after the decrement processing. After the read lock is unlocked, an update of the read count may be performed. After the read operation is completed on the second resource corresponding to the first read request, the read count is updated, that is, the read count lock may be locked first, and if the read count is greater than zero, the read count is decremented by one. In this embodiment, the read count may be understood as a counted number of received read requests, and each time a read request is received, an operation of increasing one may be performed on the read count, and each time a read operation corresponding to a read request is performed, which indicates that the read request has ended, the read count may be decreased by one.
That is, in this embodiment, a read count of zero may indicate that each read request has ended, and the write lock is unlocked only when the read count is zero, and a subsequent write requester may add the write lock to perform write operation.
In one embodiment, after locking the read count lock, the method further comprises:
and unlocking the read count lock under the condition that the read count is not zero.
The read count in this embodiment is the latest read count, and if the read count is not zero, it indicates that there is a read request that has not ended, and the read count lock is unlocked, and at this time, the write lock does not need to be unlocked, and the write lock cannot be added to other write requests. Therefore, the read-write mutual exclusion can be realized, and the read-write mutual exclusion performance is improved.
The above method is described in detail with reference to an embodiment.
Initializing a read-write lock:
the read count (m _ rc) is initialized to 0, the write count (m _ wc) is initialized to 0, and in addition, 4 mutexes are initialized, which respectively are: the counter lock comprises a read count lock (m _ rc _ lock), a write count lock (m _ wc _ lock), a read lock (m _ r _ lock) and a write lock (m _ w _ lock).
The flow for locking with writer identity is as follows:
a write count lock (m _ wc _ lock) is locked;
the write count (m _ wc) is increased by 1;
if the write count (m _ wc) is equal to 1, the read lock (m _ r _ lock) locks;
the write count lock (m _ wc _ lock) is unlocked;
the write lock (m _ w _ lock) is locked.
It should be noted that, if the write count is equal to 1 and the read lock is in the unlocked state, the read lock may be locked, and then the subsequent unlocking of the write count lock and locking of the write lock (locking of the write lock when the write lock is in the unlocked state) may be performed. If the write count is greater than 1, the read lock is not locked, and the subsequent write count lock unlocking and write lock locking may be performed.
The procedure for unlocking with writer identity is as follows:
the write lock (m _ w _ lock) is unlocked;
a write count lock (m _ wc _ lock) is locked;
if the write count (m _ wc) is greater than 0, subtracting 1 from the write count (m _ wc);
if the write count (m _ wc) is equal to 0, the read lock (m _ r _ lock) is unlocked;
the write count lock (m _ wc _ lock) is unlocked.
It should be noted that, the read lock may be unlocked when the write count is equal to 0, otherwise, the read lock may not be unlocked, and the read lock may not be locked by other read identities, so as to implement mutual exclusion between reading and writing. In addition, if the write count is not zero after being subtracted by one, the read lock is not unlocked, and the subsequent write count lock unlocking is executed.
The procedure for locking with reader identity is as follows:
the read lock (m _ r _ lock) is locked;
the read count lock (m _ rc _ lock) is locked;
the read count (m _ rc) is increased by 1;
if the read count (m _ rc) is equal to 1, the write lock (m _ w _ lock) locks;
the read count lock (m _ rc _ lock) is unlocked;
the read lock (m _ r _ lock) is unlocked.
It should be noted that, if the read count is equal to 1 and the write lock is in the unlocked state, the write lock may be locked, and then the subsequent unlocking of the read count lock and the subsequent unlocking of the read lock may be performed. In addition, if the read count is greater than 1, the write lock is not locked, and the subsequent unlocking of the read count lock and the unlocking of the read lock are performed.
The procedure for unlocking with reader identity is as follows:
the read count lock (m _ rc _ lock) is locked;
if the read count (m _ rc) is greater than 0, subtracting 1 from the read count (m _ rc);
if the read count (m _ rc) is equal to 0, the write lock (m _ w _ lock) is unlocked;
the read count lock (m _ rc _ lock) is unlocked.
It should be noted that, when the read count is equal to 0, the write lock may be unlocked, otherwise, the write lock is not unlocked, and other write identities may not lock the write lock, and may not perform write operations, thereby implementing mutual exclusion between read and write. In addition, if the read count is not zero after subtracting one, the write lock is not unlocked, and the subsequent read count lock unlocking is executed.
In the embodiment of the present application, "read and read" share: the multiple threads are locked by reading identities, and a reading count (m _ rc) is used for recording the number of the current concurrent reading threads; "read and write" mutual exclusion: the write identity locking locks the read lock (m _ r _ lock) when the write count (m _ wc) is equal to 1; the read identity locking locks the write lock (m _ w _ lock) when the read count (m _ rc) is equal to 1; "write-write" mutual exclusion: when the write identity is locked, the write lock (m _ w _ lock) is locked, and only when the write identity is unlocked, the write lock (m _ w _ lock) is unlocked; "write" takes precedence: under the condition that a write lock is added to a read identity, a write identity locking request exists, when a write count (m _ wc) is equal to 1, a read lock (m _ r _ lock) is locked, a subsequent read identity locking request cannot continuously add the read lock, the existing read identity request is processed, the write lock is released, at the moment, the released write lock can be added in the write identity locking process so as to perform subsequent write operation, at the moment, the read lock (m _ r _ lock) is locked in the write identity locking process, the subsequent read identity locking request cannot add the read lock, the write operation of the write identity is completely finished, after the read lock is released, the subsequent read identity locking request can add the read lock, and thus, write priority can be realized.
The method of the embodiment of the application can be applied to a multithreading synchronization scene in computer system application, and has a good effect in the case of a scene with less reading and writing and high writing timeliness requirement.
As shown in fig. 3, according to an embodiment of the present application, the present application further provides an apparatus 300 for implementing a read/write lock, where the apparatus includes:
a first locking counting module 301, configured to lock a writing counting lock and increment a writing count by one when a first writing request is received, where an initialization value of the writing count is zero;
a first locking module 302, configured to lock the read lock if the write count is one;
the first unlocking module 303 is used for unlocking the write count lock under the condition that the read lock is in a locked state;
the write module 304 is configured to lock the write lock and perform a write operation on a first resource corresponding to the first write request when the write count lock is in the unlocked state.
In one embodiment, the apparatus 300 further comprises:
the second unlocking module is used for unlocking the write lock after the write module executes the write operation on the first resource corresponding to the first write request;
the second locking module is used for locking the write counting lock;
the second counting module is used for subtracting one from the write count under the condition that the write count is larger than zero;
and the third unlocking module unlocks the read lock and unlocks the write count lock under the condition that the write count is zero.
In one embodiment, the apparatus 300 further comprises:
and the fourth unlocking module is used for unlocking the write count lock under the condition that the write count is not zero after the second locking module locks the write count lock.
In one embodiment, the apparatus 300 further comprises:
the second locking counting module is used for locking the reading lock and locking the reading counting lock under the condition of receiving the first reading request before the first locking counting module responds to the first writing request and locks the writing counting lock under the condition of receiving the first writing request;
the third counting module is used for increasing the read count by one, wherein the initialization value of the read count is zero;
the third locking module is used for locking the write lock under the condition that the read count is one;
the fifth unlocking module is used for unlocking the reading counting lock under the condition that the writing lock is in a locked state;
and the sixth unlocking module is used for unlocking the reading lock under the condition that the reading counting lock is in an unlocking state.
In one embodiment, the apparatus 300 further comprises:
the fourth locking module is used for locking the reading counting lock after the sixth unlocking module unlocks the reading lock;
the fourth counting module is used for subtracting one from the read count under the condition that the read count is larger than zero;
and the seventh unlocking module is used for unlocking the write lock and unlocking the read count lock under the condition that the read count is zero.
In one embodiment, the apparatus 300 further comprises:
and the eighth unlocking module is used for unlocking the reading counting lock under the condition that the reading counting is not zero after the fourth locking module locks the reading counting lock.
The device for implementing a read-write lock according to each embodiment is a device for implementing the method for implementing a read-write lock according to each embodiment, and has corresponding technical features and technical effects, which are not described herein again.
There is also provided, in accordance with an embodiment of the present application, an electronic device, a readable storage medium, and a computer program product.
The non-transitory computer readable storage medium of the embodiments of the present application stores computer instructions for causing a computer to execute an implementation method of the read-write lock provided by the present application.
The computer program product of the embodiments of the present application includes a computer program, where the computer program is used to enable a computer to execute an implementation method of a read-write lock provided in the embodiments of the present application.
FIG. 4 shows a schematic block diagram of an example electronic device 400 that may be used to implement embodiments of the present application. Electronic devices are intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions, are meant to be examples only, and are not meant to limit implementations of the present application that are described and/or claimed herein.
As shown in fig. 4, the electronic device 400 includes a computing unit 401 that can perform various appropriate actions and processes according to a computer program stored in a Read Only Memory (ROM)402 or a computer program loaded from a storage unit 408 into a Random Access Memory (RAM) 403. In the RAM403, various programs and data required for the operation of the device 400 can also be stored. The computing unit 401, ROM 402, and RAM403 are connected to each other via a bus 404. An input/output (I/O) interface 405 is also connected to bus 404.
A number of components in the electronic device 400 are connected to the I/O interface 405, including: an input unit 406 such as a keyboard, a mouse, or the like; an output unit 407 such as various types of displays, speakers, and the like; a storage unit 408 such as a magnetic disk, optical disk, or the like; and a communication unit 409 such as a network card, modem, wireless communication transceiver, etc. The communication unit 409 allows the electronic device 400 to exchange information/data with other devices via a computer network such as the internet and/or various telecommunication networks.
Computing unit 401 may be a variety of general and/or special purpose processing components with processing and computing capabilities. Some examples of computing unit 401 include, but are not limited to, a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), various specialized artificial intelligence (I) computing chips, various computing units running machine learning model algorithms, a Digital Signal Processor (DSP), and any suitable processor, controller, microcontroller, or the like. The computing unit 401 executes the various methods and processes described above, such as the implementation of a read-write lock. For example, in some embodiments, the method of implementing a read-write lock may be implemented as a computer software program tangibly embodied in a machine-readable medium, such as storage unit 408. In some embodiments, part or all of the computer program may be loaded and/or installed onto the device 400 via the ROM 402 and/or the communication unit 409. When loaded into RAM403 and executed by computing unit 401, may perform one or more of the steps of the above-described implementation method of a read-write lock. Alternatively, in other embodiments, the computing unit 401 may be configured to perform the implementation of the read-write lock by any other suitable means (e.g., by means of firmware). Various implementations of the systems and techniques described here above may be implemented in digital electronic circuitry, integrated circuitry, Field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), system on a chip (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and/or combinations thereof. These various embodiments may include: implemented in one or more computer programs that are executable and/or interpretable on a programmable system including at least one programmable processor, which may be special or general purpose, receiving data and instructions from, and transmitting data and instructions to, a storage system, at least one input device, and at least one output device.
Program code for implementing the methods of the present application may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus, such that the program codes, when executed by the processor or controller, cause the functions/operations specified in the flowchart and/or block diagram to be performed. The program code may execute entirely on the machine, partly on the machine, as a stand-alone software package partly on the machine and partly on a remote machine or entirely on the remote machine or server.
In the context of this application, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to a user; and a keyboard and a pointing device (e.g., a mouse or a trackball) by which a user can provide input to the computer. Other kinds of devices may also be used to provide for interaction with a user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form, including acoustic, speech, or tactile input.
The systems and techniques described here can be implemented in a computing system that includes a back-end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front-end component (e.g., a user computer having a graphical user interface or a web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back-end, middleware, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: local Area Networks (LANs), Wide Area Networks (WANs), the internet, and blockchain networks.
The computer system may include a second electronic device and a server. The second electronic device and the server are generally remote from each other and typically interact through a communication network. The relationship of second electronic device and server arises by virtue of computer programs running on the respective computers and having a second electronic device-server relationship to each other. The Server can be a cloud Server, also called a cloud computing Server or a cloud host, and is a host product in a cloud computing service system, so as to solve the defects of high management difficulty and weak service expansibility in the traditional physical host and VPS service ("Virtual Private Server", or simply "VPS"). The server may also be a server of a distributed system, or a server incorporating a blockchain.
It should be understood that various forms of the flows shown above may be used, with steps reordered, added, or deleted. For example, the steps described in the present application may be executed in parallel, sequentially, or in different orders, and the present invention is not limited thereto as long as the desired results of the technical solutions disclosed in the present application can be achieved.
The above-described embodiments should not be construed as limiting the scope of the present application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may be made in accordance with design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims (15)

1. A method for implementing a read-write lock, the method comprising:
under the condition that a first write request is received, locking a write count lock, and increasing a write count by one, wherein the initialization value of the write count is zero;
locking the read lock when the write count is one;
when the read lock is in a locked state, unlocking the write count lock;
and under the condition that the write count lock is in an unlocked state, locking the write lock, and performing write operation on the first resource corresponding to the first write request.
2. The method of claim 1, wherein after the performing the write operation on the first resource corresponding to the first write request, further comprising:
unlocking the write lock;
locking the write count lock;
decrementing the write count by one if the write count is greater than zero;
and unlocking the read lock and unlocking the write count lock under the condition that the write count is zero.
3. The method of claim 2, wherein after locking the write count lock, further comprising:
and unlocking the write count lock under the condition that the write count is not zero.
4. The method of claim 1, wherein said, in the event a first write request is received, prior to locking a write count lock in response to the first write request, further comprises:
locking the read lock and locking a read count lock if a first read request is received;
incrementing a read count by one, wherein an initialization value of the read count is zero;
locking the write lock if the read count is one;
when the write lock is in a locked state, unlocking the read counter lock;
and unlocking the reading lock under the condition that the reading counting lock is in an unlocked state.
5. The method of claim 4, wherein after the unlocking the read lock, further comprising:
locking the counter lock;
decrementing the read count by one if the read count is greater than zero;
and unlocking the write lock and the read count lock if the read count is zero.
6. The method of claim 5, wherein after locking the readcount lock, further comprising:
and unlocking the read count lock under the condition that the read count is not zero.
7. An apparatus for implementing a read-write lock, the apparatus comprising:
the first locking counting module is used for locking a writing counting lock and increasing the writing counting by one under the condition of receiving a first writing request, wherein the initialization value of the writing counting is zero;
the first locking module is used for locking the read lock under the condition that the write count is one;
the first unlocking module is used for unlocking the write counting lock under the condition that the read lock is in a locked state;
and the writing module is used for locking the writing lock and performing writing operation on the first resource corresponding to the first writing request under the condition that the writing counting lock is in an unlocked state.
8. The apparatus of claim 7, further comprising:
the second unlocking module is used for unlocking the write lock after the write module executes write operation on the first resource corresponding to the first write request;
the second locking module is used for locking the write counting lock;
a second counting module for decrementing the write count by one if the write count is greater than zero;
and the third unlocking module unlocks the read lock and unlocks the write count lock under the condition that the write count is zero.
9. The apparatus of claim 8, further comprising:
and the fourth unlocking module is used for unlocking the write count lock under the condition that the write count is not zero after the second locking module locks the write count lock.
10. The apparatus of claim 7, further comprising:
a second locking counting module, configured to lock the read lock and lock the read count lock when a first read request is received before locking the write count lock in response to a first write request when the first write request is received by the first locking counting module;
the third counting module is used for increasing the read count by one, wherein the initialization value of the read count is zero;
a third locking module, configured to lock the write lock if the read count is one;
the fifth unlocking module is used for unlocking the reading counting lock under the condition that the writing lock is in a locked state;
and the sixth unlocking module is used for unlocking the reading lock under the condition that the reading counting lock is in an unlocking state.
11. The apparatus of claim 10, further comprising:
the fourth locking module is used for locking the reading counting lock after the sixth unlocking module unlocks the reading lock;
a fourth counting module for decrementing the read count by one if the read count is greater than zero;
and the seventh unlocking module is used for unlocking the write lock and unlocking the read count lock under the condition that the read count is zero.
12. The apparatus of claim 11, further comprising:
and the eighth unlocking module is used for unlocking the counter reading lock under the condition that the counter reading is not zero after the fourth locking module locks the counter reading lock.
13. An electronic device, comprising:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform a method of implementing a read-write lock as claimed in any one of claims 1 to 6.
14. A non-transitory computer readable storage medium having stored thereon computer instructions for causing the computer to perform a method of implementing a read-write lock as claimed in any one of claims 1 to 6.
15. A computer program product comprising a computer program which, when being executed by a processor, carries out a method of implementing a read-write lock according to any one of claims 1 to 6.
CN202110280807.5A 2021-03-16 2021-03-16 Method and device for realizing read-write lock and electronic equipment Active CN113010325B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110280807.5A CN113010325B (en) 2021-03-16 2021-03-16 Method and device for realizing read-write lock and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110280807.5A CN113010325B (en) 2021-03-16 2021-03-16 Method and device for realizing read-write lock and electronic equipment

Publications (2)

Publication Number Publication Date
CN113010325A true CN113010325A (en) 2021-06-22
CN113010325B CN113010325B (en) 2023-07-25

Family

ID=76408187

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110280807.5A Active CN113010325B (en) 2021-03-16 2021-03-16 Method and device for realizing read-write lock and electronic equipment

Country Status (1)

Country Link
CN (1) CN113010325B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113609339A (en) * 2021-08-05 2021-11-05 北京汇钧科技有限公司 Method and device for improving performance of intelligent network card
CN114205260A (en) * 2021-11-08 2022-03-18 南方电网深圳数字电网研究院有限公司 TCP (Transmission control protocol) connection activity detection method and device based on GO (GO) language

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105224251A (en) * 2015-09-28 2016-01-06 浪潮(北京)电子信息产业有限公司 A kind of Read-Write Locks optimization method based on Linux and system
US20160098361A1 (en) * 2014-10-07 2016-04-07 Check Point Software Technologies Ltd. Optimization of Data Locks for Improved Write Lock Performance and CPU Cache usage in Mulit Core Architectures
US20200073728A1 (en) * 2018-08-28 2020-03-05 Baidu Online Network Technology (Beijing) Co., Ltd. Method and device for implementing read-write lock reentry, terminal and storage medium
CN111143080A (en) * 2019-12-31 2020-05-12 联想(北京)有限公司 Read-write lock optimization method, device and system and storage medium
CN111782669A (en) * 2020-06-28 2020-10-16 百度在线网络技术(北京)有限公司 Method and device for realizing distributed lock and electronic equipment
CN112416556A (en) * 2020-11-23 2021-02-26 西安西热电站信息技术有限公司 Data read-write priority balancing method, system, device and storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20160098361A1 (en) * 2014-10-07 2016-04-07 Check Point Software Technologies Ltd. Optimization of Data Locks for Improved Write Lock Performance and CPU Cache usage in Mulit Core Architectures
CN105224251A (en) * 2015-09-28 2016-01-06 浪潮(北京)电子信息产业有限公司 A kind of Read-Write Locks optimization method based on Linux and system
US20200073728A1 (en) * 2018-08-28 2020-03-05 Baidu Online Network Technology (Beijing) Co., Ltd. Method and device for implementing read-write lock reentry, terminal and storage medium
CN111143080A (en) * 2019-12-31 2020-05-12 联想(北京)有限公司 Read-write lock optimization method, device and system and storage medium
CN111782669A (en) * 2020-06-28 2020-10-16 百度在线网络技术(北京)有限公司 Method and device for realizing distributed lock and electronic equipment
CN112416556A (en) * 2020-11-23 2021-02-26 西安西热电站信息技术有限公司 Data read-write priority balancing method, system, device and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
孙建杰;陈佳品;: "临界区读写锁的实现", 计算机与现代化, no. 09, pages 215 - 219 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113609339A (en) * 2021-08-05 2021-11-05 北京汇钧科技有限公司 Method and device for improving performance of intelligent network card
CN114205260A (en) * 2021-11-08 2022-03-18 南方电网深圳数字电网研究院有限公司 TCP (Transmission control protocol) connection activity detection method and device based on GO (GO) language

Also Published As

Publication number Publication date
CN113010325B (en) 2023-07-25

Similar Documents

Publication Publication Date Title
CN109271258B (en) Method, device, terminal and storage medium for realizing re-entry of read-write lock
US6601120B1 (en) System, method and computer program product for implementing scalable multi-reader/single-writer locks
CN113010325B (en) Method and device for realizing read-write lock and electronic equipment
CN111475262B (en) Transaction request processing method, device, equipment and medium in blockchain
CN113835901A (en) Read lock operation method, write lock operation method and system
CN112416556B (en) Data read-write priority balancing method, system, device and storage medium
US20230120580A1 (en) Access control method and apparatus for shared memory, electronic device and autonomous vehicle
CN110352406A (en) Without lock reference count
CN111263930B (en) Preventing long-running transaction holding record locks
CN109376165A (en) The implementation method and device and computer readable storage medium of memory database lock
US20180232304A1 (en) System and method to reduce overhead of reference counting
US20110145515A1 (en) Method for modifying a shared data queue and processor configured to implement same
US20140181809A1 (en) Creating multiple rules for a device to allow concurrent access to the device by different virtual machines
US20130117522A1 (en) Inter-Process Memory Management
US9250976B2 (en) Tiered locking of resources
US9535713B2 (en) Manipulating rules for adding new devices
CN115114612A (en) Access processing method, device, electronic equipment and storage medium
US9081630B2 (en) Hardware-implemented semaphore for resource access based on presence of a memory buffer in a memory pool
CN111143351B (en) IMSI data management method and equipment
CN111913810A (en) Task execution method, device, equipment and storage medium under multi-thread scene
CN111143232A (en) Method, apparatus and computer program product for storing metadata
CN109976675B (en) Data updating and reading method, device, equipment and storage medium
CN115599307B (en) Data access method, device, electronic equipment and storage medium
CN114356589B (en) Multi-writer and multi-reader data storage and reading method, device and equipment
CN116431561B (en) Data synchronization method, device, equipment and medium based on heterogeneous many-core accelerator card

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