CN102999378B - A kind of read-write lock implement method - Google Patents

A kind of read-write lock implement method Download PDF

Info

Publication number
CN102999378B
CN102999378B CN201210511370.2A CN201210511370A CN102999378B CN 102999378 B CN102999378 B CN 102999378B CN 201210511370 A CN201210511370 A CN 201210511370A CN 102999378 B CN102999378 B CN 102999378B
Authority
CN
China
Prior art keywords
read
write
lock
critical section
thread
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
CN201210511370.2A
Other languages
Chinese (zh)
Other versions
CN102999378A (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.)
Institute of Software of CAS
Original Assignee
Institute of Software of CAS
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 Institute of Software of CAS filed Critical Institute of Software of CAS
Priority to CN201210511370.2A priority Critical patent/CN102999378B/en
Publication of CN102999378A publication Critical patent/CN102999378A/en
Application granted granted Critical
Publication of CN102999378B publication Critical patent/CN102999378B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Landscapes

  • Storage Device Security (AREA)

Abstract

The present invention relates to a kind of synchronous method being realized cross-thread by Read-Write Locks, its step comprises: 1) initialization Read-Write Locks, will read critical section and write critical section to be initialized as and can to get the hang of, and writes notification event and is initialized as the state of being triggered; 2) read thread when multiple and/or write thread Concurrency Access shared resource, protect key code in described shared resource according to critical section, Read-Write Locks is set; 3) return Read-Write Locks information and complete Read-Write Locks setting, realize resource sharing.The present invention proposes the method realizing Read-Write Locks with critical section and composition of matter, have and be simple and easy to use, light weight feature fast, this method adopts critical section as main mutex tool simultaneously, avoid the expense using User Status during kernel state object frequently to switch, write thread can not infinitely wait for, only need to wait until read thread all complete operations when lock is write in application, and can not be applied for that the thread of read lock seizes shared resource below, substantially increase the performance of Read-Write Locks.

Description

A kind of read-write lock implement method
Technical field
The present invention relates to read-write lock implement method, particularly relate to the synchronous method of the cross-thread based on Read-Write Locks, the technical field of software development of various software systems research and development can be applied to, belong to areas of information technology
Background technology
In areas of information technology, software development relates to a lot of technology, comprise memory data organization management, message communicating, index technology, synchronization and mutex technology etc., the present invention pays close attention to the simultaneous techniques between software development thread, the simultaneous techniques of cross-thread is related in some specific software systems R&D processes, and to synchronous operation, there is higher real-time and the requirement of high efficiency, and such as: rdal-time DBMS, relational database management system, middleware etc.
Existing concurrent synchronization and mutex object (can refer to the illustrative material http://msdn.microsoft.com/en-us/library/windows/desktop/ms68426 6%28v=vs.85%29.aspx of official of Microsoft about Mutex object) mainly contains mutex (Mutex), semaphore (Semaphore), event (Event), critical section (Critical Section) (the illustrative material http://msdn.microsoft.com/en-us/library/windows/desktop/ms68253 0%28v=vs.85%29.aspx of official of Microsoft about critical section object can be referred to) etc., kernel state object and User space object can be divided into according to level belonging to object, also inter process synchronization technology and cross-thread simultaneous techniques can be divided into according to purposes.Critical section is the easy way ensureing at a time to only have a thread energy visit data, is User space object; Mutex is similar with critical section, and the thread only having Mutex object just has the authority of access resources, but more complicated than critical section, belongs to kernel state object, can realize the safe sharing to resource between different application process.Semaphore is used for controlling to have limited quantity user resources to one and carries out Concurrency Access, comprise one and use counting, maximum resource number, Current resource counting, maximum resource number represents that the maximum resource quantity that semaphore can control, Current resource number represent the current available resource quantity of signal; Event is used for notifying that thread has some event to occur, thus wakes this thread execution subsequent tasks up.Mutex, semaphore, event are all mutual exclusion or the simultaneous techniques of kernel state, therefore also can be used as process and cross-thread is synchronous.It is synchronous that critical section can only be used as cross-thread, but owing to being User space technology, the context switch overhead not from kernel state to User space, therefore its performance is better than mutex.
Concurrent requests a large amount of in software systems can be divided into a read request and read-write requests, and the former can operate shared resource simultaneously, and the latter exclusively must occupy shared resource, does not allow other people to access.In this case mutex, semaphore, event etc. is directly used to be all inappropriate, there are larger kernel state and the handover overhead of User space on the one hand, do not distinguish the feature of concurrent reading and writing request on the other hand, cause the read request of concurrence performance to become serial and perform, greatly reduce system effectiveness.Therefore, need for a large amount of concurrent reading and writing request, well-designed a kind of cross-thread synchronization mechanism, realizes maximized concurrence performance, thus improves the overall performance of software systems.
Summary of the invention
The present invention is directed to rdal-time DBMS, relational database management system, the synchronous real-time demand of cross-thread of middleware and performance requirement, design and propose a kind of synchronous method of multi-thread access shared resource real-time.
The technical problem to be solved in the present invention
In R & D Software Development systematic procedure, usually can run into the problem of multi-thread concurrent access shared resource, if concurrency is lower, just can realize with simple mutex (Mutex).But when concurrency is higher, some high performance software systems also require that the synchronous operation between multithreading has higher performance.Concurrent multithreading can be divided into two classes according to whether revising shared resource: read-only thread and amendment thread, a good Multi-thread synchronization technology should allow multiple read-only thread can access shared resource simultaneously, secondly more new thread should can obtain shared resource after the current read-only thread complete operation of wait at once, there will not be follow-up read-only thread to seize the situation of shared resource from amendment thread.Due to mainstream operation system as Windows 2000, WindowsXP and Windows 2003 Server do not have built-in Read-Write Locks, therefore usually need developer to expend the Read-Write Locks of longer time Design and implementation cross-thread, and the final effect realized might not meet the software systems high performance requirements synchronous to cross-thread.
The present invention is directed to this kind of technical matters, the Read-Write Locks solution proposing a kind of general, real-time cross-thread is as follows, gives the specific implementation details of Read-Write Locks.On the one hand, the time of developer can be greatly reduced, on the other hand, software systems can be met and carry out synchronous performance requirement between multiple read-write thread.
A kind of read-write lock implement method, its step comprises:
1) initialization Read-Write Locks, reads critical section according to Read-Write Locks building component, writes critical section and writes notification event three objects, described in read critical section and described critical section of writing is initialized as and can gets the hang of, described in write notification event and be initialized as the state of being triggered;
2) read thread when multiple and/or write thread Concurrency Access shared resource, protect key code in described shared resource according to described critical section, Read-Write Locks is set;
When 2-1) applying for read lock interface, be introduced into and write critical section and enter again and read critical section, stop the operation of write or read thread according to read lock number, write notification event simultaneously and be set to non-toggle state; During release read lock interface, enter and read critical section, if now read lock number is 0, described notification event of writing is set to trigger state simultaneously;
2-2) application is when writing lock interface, is introduced into write critical section and enter and read critical section, according to the operation writing lock number and stop write or read thread, writes notification event and be set to non-toggle state simultaneously; When lock interface is write in release; Enter and read critical section, write notification event simultaneously and be set to trigger state;
3) travel through described 2-1) to 2-2) return Read-Write Locks information complete Read-Write Locks arrange, realize resource sharing.
Define in described Read-Write Locks assembly: open member's constructed fuction CReadWriteLock and destructor function ~ CReadWriteLock; Application read lock interface function ReadLock and release read lock interface function ReadUnlock; Lock WriteLock interface function is write in application and lock interface function WriteUnlock is write in release.
Four privately owned members are also defined: maximum read lock number m_ulMaxReadCount in described Read-Write Locks assembly; Read critical section m_hReadCriticalSection; Write critical section m_hWriteCriticalSection; Write notification event m_hWriteNotifyEvent.
Described maximum read lock number m_ulMaxReadCount is defaulted as 5.
Described step 2-2) in application when writing lock, the waiting-timeout time that a unit is Millisecond is set, obtains yet after exceeding the described stand-by period and write lock and namely return failure.
Further, described step 2-1) in, stop the method for operating of write or read thread as follows according to read lock number:
After 2-1-1) entering described two critical sections, if find, current read lock number reaches maximum number, then return failure, otherwise illustrates that current thread can proceed read lock operation;
2-1-2) as described step 2-1-1) in current read lock number do not reach maximum number, current read lock number can be increased, if increase after read lock number be greater than 0, then can not allow to write thread and operate.
Further, described step 2-2) in, stop the method for the operation of write or read thread as follows according to writing lock number:
2-2-1) be introduced into and write critical section and stop other thread application read lock or write lock, then within the stand-by period of setting etc. notification event to be written to guarantee not having other thread to carry out read-write operation to shared resource;
If 2-2-2) write notification event within the stand-by period of described setting not to be triggered, then wait timeout, returns failure; If write notification event to be at the appointed time triggered, then illustrate and now do not read thread, what acquisition was exclusive writes lock, then will write notification event and be set to non-toggle state, and be latched in as long as have read lock or write, and does not just allow new writing to lock application, returns successfully;
2-2-3) described step 2-2-2) if return successfully, application is write locking wire journey and can not be left and write critical section, when returning failure, the thread that lock is write in application will leave at once writes critical section.
Further, in described release read lock process, only enter and read critical section, do not enter and write critical section, then read lock number is subtracted 1, when reducing to 0, notification event being set to trigger state, exiting and reading critical section;
Further, described release is write in lock process, first will write notification event and be set to trigger state, then exits and write critical section.
Further, described Read-Write Locks does not have to operate in the operating system of built-in Read-Write Locks in Windows 2000, Windows XP and Windows 2003 Server.
Read-Write Locks solution relates generally to three parts, and Read-Write Locks carries out concurrent request, parallel processing: 1) Read-Write Locks initialization procedure; 2) application and release read lock; 3) lock is write in application and release.This three part is included in the Read-Write Locks class CReadWriteLock of the present invention's realization; open member function as class gives outside use; inner employing two critical section objects are protected reading and writing key code respectively, and use case object notifies the state writing lock.Because the speed of critical section is far away faster than mutex, therefore adopt critical section amount can obtain higher operational efficiency.The read lock number that class CReadWriteLock inside uses a variable record current, all will judge the relation with maximum read lock number when application, release lock, thus determine the flow process that application is locked.
In Read-Write Locks initialization procedure, establishment and initialization are read critical section, write critical section and are write notification event three objects respectively, reading and writing critical section is initialized as and can gets the hang of, write notification event and be initialized as the state of being triggered, thus can accept to write lock application, then the current read lock number of initialization is 0, namely also can receive read lock application.
In application read lock process, need read and revise current read lock number, therefore use critical section to protect this section of code; critical section is divided into two, and one is write critical section, is used for protecting the operation writing phase-locked pass; one is read critical section, is used for protecting the operation that read lock is relevant.Be exclusive owing to writing lock, therefore must be introduced into and write critical section, then enter and read critical section, order when exiting is then contrary, first exits and reads critical section, then exits and write critical section, so just can avoid the situation occurring deadlock.After entering two critical sections, if find that current read lock number reaches maximum number, then return failure, otherwise illustrate that current thread can also obtain read lock, so increase current read lock number, if read lock number is greater than 0 after increasing, then can not allows to write thread and operate, now can also continue to increase read lock number, as long as be no more than the maximum read lock number of setting.Having during read lock does not just allow application to write lock.So also will arrange, to write notification event be non-toggle state, stops other thread to obtain and write lock.
In release read lock process, because now read lock number is greater than 0, thread can not be had to obtain and to write lock, therefore only need to enter to read critical section, and write critical section without the need to entering, then read lock number is subtracted 1, illustrating when reducing to 0 does not now have thread to carry out read operation to shared resource, therefore thread can be allowed to carry out write operation, by arrange write notification event be trigger state wake up waiting for write thread.
Write in lock process in application, first to enter and write critical section to stop other thread application read lock or to write lock, namely guarantee to be seized by the thread of follow-up application Read-Write Locks, etc. then notification event to be written is to guarantee not having other thread to carry out read-write operation to shared resource, this waiting process can arrange time-out time, to improve the success ratio that lock is write in application, is not triggered if write notification event at the appointed time, then wait timeout, returns failure.If write notification event to be at the appointed time triggered, then illustrate and now do not read thread, applicant can obtain and exclusive write lock, then will write notification event and be set to non-toggle state, be latched in as long as return successfully to have read lock or write, just do not allow new writing to lock application.Return successfully.If return successfully, in order to ensure the exclusivity writing lock, application is write locking wire journey and can not be left and write critical section, makes the thread of other application Read-Write Locks write this step of critical section and just get clogged entering; When returning unsuccessfully, the thread applying for writing lock will leave at once writes critical section.
Write in lock process in release, write critical section owing to just entering when applying for read lock but do not withdraw, therefore just do not need to enter when lock is write in release and write critical section, as long as notification event first will be write be set to trigger state, then exit and write critical section.
Beneficial effect of the present invention
Can find out according to above technical solution, the present invention has following beneficial effect:
1, the present invention proposes the method realizing Read-Write Locks with critical section and composition of matter, have and be simple and easy to use, light weight feature fast.This method adopts critical section as main mutex tool, avoids the expense using User Status during kernel state object frequently to switch, substantially increases the performance of Read-Write Locks.
2, the present invention is that the scene read thread in a large number and write thread has on a small quantity done following optimization: read thread and read not conflict between thread, and apply for that when existing read lock the expense of read lock is very little again; Write thread can not infinitely wait for, only need to wait until read thread all complete operations when lock is write in application, and can not be applied for that the thread of read lock seizes shared resource below.
3, the invention provides a Read-Write Locks and realize class, there is succinct application lock, the calling interface of release lock, can be integrated into very easily in various software systems.
Accompanying drawing explanation
Fig. 1 is the process flow diagram applying for read lock in read-write lock implement method one embodiment of the present invention;
Fig. 2 is the process flow diagram discharging read lock in read-write lock implement method one embodiment of the present invention;
Fig. 3 is the process flow diagram that in read-write lock implement method one embodiment of the present invention, lock is write in application;
Fig. 4 is the process flow diagram that in read-write lock implement method one embodiment of the present invention, lock is write in release;
Fig. 5 is the schematic flow sheet of read-write lock implement method of the present invention.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present invention, be clearly and completely described, be understandable that the technical scheme in the embodiment of the present invention, described embodiment is only the present invention's part embodiment, instead of whole embodiments.Based on the embodiment in the present invention, those skilled in the art, not making the every other embodiment obtained under creative work prerequisite, belong to the scope of protection of the invention.
Be the schematic flow sheet of read-write lock implement method of the present invention as shown in Figure 5, Read-Write Locks solution relates generally to three parts, and Read-Write Locks carries out concurrent request, parallel processing: 1) Read-Write Locks initialization procedure; 2) application and release read lock; 3) lock is write in application and release.
Read-Write Locks assembly
Read-Write Locks class is defined as follows:
[1]class CReadWriteLock{
[2]public:
[3]CReadWriteLock(ULONG ulMaxReadCount=5);
[4]virtual~CReadWriteLock();
[5]virtual BOOL ReadLock();
[6]virtual void ReadUnLock();
[7]virtual BOOL WriteLock(ULONG ulWaitMSTime=INFINITE);
[8]virtual void WriteUnLock();
[9]protected:
[10]ULONG m_ulMaxReadCount;
[11]CRITICAL_SECTION m_hReadCriticalSection;
[12]CRITICAL_SECTION m_hWriteCriticalSection;
[13]HANDLE m_hWriteNotifyEvent;
[14]};
There is defined six public method: CReadWriteLock, ~ CReadWriteLock is constructed fuction and destructor function; ReadLock, ReadUnlock are the interface functions of application read lock and release read lock; WriteLock, WriteUnlock are that application is write lock and discharges the interface function writing lock.
Also define four privately owned member: m_ulMaxReadCount in addition and represent maximum read lock number, be defaulted as 5; M_hReadCriticalSection is the critical section protected read operation, namely reads critical section, and m_hWriteCriticalSection is the critical section protected write operation, namely writes critical section; M_hWriteNotifyEvent writes notification event, is used to indicate the state writing lock.
Initialization interface method realizes
The constructed fuction of class CReadWriteLock is initialization interface method, is defined as:
CReadWriteLock::CReadWriteLock(ULONG ulMaxReadCount=5);
Wherein parameter ulMaxReadCount is used for arranging the maximum concurrent read lock number that Read-Write Locks allows, and is defaulted as 5.In general, maximum concurrent read lock number should not arrange too large, otherwise can affect performance.
The specific implementation code of initialization interface method is as follows:
[1]::InitializeCriticalSection(&m_hReadCriticalSection);
[2]::InitializeCriticalSection(&m_hWriteCriticalSection);
[3]m_hWriteNotifyEvent=::CreateEvent(0,TRUE,TRUE,0);
[4]m_ulReadCount=ulMaxReadCount;
The method mainly carries out initialization to the object reference system interface such as critical section, event that Read-Write Locks is used, and writes notification event and is initialized to trigger state, arrange maximum concurrent read lock number simultaneously.
The process flow diagram applying for read lock in read-write lock implement method one embodiment of the present invention as shown in Figure 1; First rreturn value is set to TRUE, enters and write critical section, after entering two critical sections, start read lock number and write notification event and operate, if then judge that read lock number arrives maximum number, then rreturn value is set to FALSE, otherwise read lock number is increased by 1.Then judge whether read lock number is 1, if it is illustrates just there has been thread application read lock, therefore should refuse other thread application and write lock, therefore resetting and writing notification event is non-toggle state; After completing aforesaid operations, leave critical section; Finally the rreturn value of setting is returned to caller.
Below apply for that read lock interface method specifically defines during application read lock interface method realizes:
BOOL CReadWriteLock::ReadLock();
If the success of application read lock, then return TRUE; If the failure of application read lock, then return FALSE.
Application read lock interface method specific implementation code is as follows:
[1]BOOL bSucc=TRUE;
[2]::EnterCriticalSection(&m_hWriteCriticalSection);
[3]::EnterCriticalSection(&m_hReadCriticalSection);
[4]if(m_ulReadCount>=m_ulMaxReadCount)
[5] bSucc=FALSE;
[6]else{
[7]m_ulReadCount++;
[8]if(m_ulReadCount==1)
[9]::ResetEvent(m_hWriteNotifyEvent);
[10]}
[11]::LeaveCriticalSection(&m_hReadCriticalSection);
[12]::LeaveCriticalSection(&m_hWriteCriticalSection);
[13]return bSucc;
Wherein, rreturn value is first set to TRUE by the 1st row; 2nd is advanced into and writes critical section, and the 3rd advances studies in critical section; After entering two critical sections, start read lock number and write notification event and operate, if the 4th row judges that read lock number arrives maximum number, then be about to rreturn value the 5th and be set to FALSE, otherwise the 7th be about to read lock number increase by 1, then eighth row judges whether read lock number is 1, if it is illustrates just there has been thread application read lock, therefore should refuse other thread application and write lock, therefore writing notification event in the 9th row replacement is non-toggle state; After completing aforesaid operations, the 11st, 12 row leave critical section; Finally the rreturn value of setting is returned to caller.
Be the process flow diagram discharging read lock in read-write lock implement method one embodiment of the present invention shown in Fig. 2, application enters reads critical section; After entering critical section, read lock number is subtracted 1; Then judge read lock number, if equal 0, illustrate now do not have read lock, arranging and writing notification event is trigger state, the thread applying for writing lock can be obtained and write lock; Leave and read critical section.
Below that during release read lock interface method realizes, release read lock interface method specifically defines:
void CReadWriteLock::ReadUnLock()
The method inside is relatively simple, does not define rreturn value.
The specific implementation code of release read lock interface method is as follows:
[1]::EnterCriticalSection(&m_hReadCriticalSection);
[2]m_ulReadCount--;
[3]if(m_ulReadCount==0)
[4] ::SetEvent(m_hWriteNotifyEvent);
[5]::LeaveCriticalSection(&m_hReadCriticalSection);
Wherein the 1st row application enters and reads critical section; After entering critical section, read lock number is then subtracted 1 by the 2nd row; 3rd row judges read lock number, if equal 0, illustrates now do not have read lock, and therefore writing notification event in the 4th row setting is trigger state, the thread applying for writing lock can be obtained and write lock; 5th row leaves to read critical section.
Be the process flow diagram that in read-write lock implement method one embodiment of the present invention, lock is write in application shown in Fig. 3, wherein, application enters writes critical section; Enter after writing critical section, the wait timeout time is provided with etc. notification event to be written, if write notification event to be at the appointed time triggered, successfully return, now do not exit critical section, this is because write operation is exclusive, do not allow other thread to have simultaneously and write lock, therefore the thread of other application Read-Write Locks is stoped and write this initial step of critical section entering; If write notification event at the appointed time not to be triggered, this means that reading thread during this period of time does not complete all operations, therefore can not carry out write operation, write critical section so exit, so that follow-up thread application Read-Write Locks, then return FALSE.
Below the concrete definition that application writes that lock interface method is write in lock interface method realization application:
BOOL CReadWriteLock::WriteLock(ULONG ulWaitMSTime);
Wherein parameter ulWaitMSTime is the stand-by period that lock is write in application, and unit is millisecond, still fails to apply for successfully if exceed this time, shows this time application operation overtime failure, returns FALSE; If apply for successfully, then returning TRUE at the appointed time.
The specific implementation code that lock interface method is write in application is as follows:
[1]::EnterCriticalSection(&m_hWriteCriticalSection);
[2]if(WAIT_OBJECT_0==::WaitForSingleObject(m_hWriteNotifyEvent,ulWaitMSTime))
[3] return TRUE;
[4]else{
[5] ::LeaveCriticalSection(&m_hWriteCriticalSection);
[6] return FALSE;
[7]}
Wherein, the 1st row application enters and writes critical section; Enter after writing critical section, be provided with the wait timeout time at notification events to be written such as the 2nd row, if write notification event to be at the appointed time triggered, successfully return at the 3rd row, now do not exit critical section, this is because write operation is exclusive, do not allow other thread to have simultaneously and write lock, therefore the thread of other application Read-Write Locks is stoped and write this initial step of critical section entering; If write notification event at the appointed time not to be triggered, this means that reading thread during this period of time does not complete all operations, therefore can not carry out write operation, write critical section so exit at the 5th row, so that follow-up thread application Read-Write Locks, then return FALSE at the 6th row.
Be the process flow diagram that in read-write lock implement method one embodiment of the present invention, lock is write in release shown in Fig. 4, resetting and writing notification event is trigger state, then exits the critical section of protection write operation, so that follow-up thread application Read-Write Locks.
Below the concrete definition that release writes that lock interface method is write in lock interface method realization release:
void CReadWriteLock::WriteUnLock();
The method inside is relatively simple, does not define rreturn value.
The specific code that lock interface method is write in release is as follows:
[1]::SetEvent(m_hWriteNotifyEvent);
[2]::LeaveCriticalSection(&m_hWriteCriticalSection);
Wherein the 1st row resets that to write notification event be trigger state, and then the 2nd row exits the critical section of protection write operation, so that follow-up thread application Read-Write Locks.

Claims (9)

1. a read-write lock implement method, its step comprises:
1) initialization Read-Write Locks, reads critical section according to Read-Write Locks building component, writes critical section and writes notification event three objects, described in read critical section and described critical section of writing is initialized as and can gets the hang of, described in write notification event and be initialized as the state of being triggered;
2) read thread when multiple and/or write thread Concurrency Access shared resource, read critical section and write critical section to protect key code in described shared resource according to described, Read-Write Locks is set;
When 2-1) applying for read lock interface, be introduced into and write critical section and enter again and read critical section, stop the operation of write or read thread according to read lock number, write notification event simultaneously and be set to non-toggle state; During release read lock interface, enter and read critical section, if read lock number is 0, described notification event of writing is set to trigger state simultaneously;
2-2) application is when writing lock interface, is introduced into write critical section and enter and read critical section, according to the operation writing lock number and stop write or read thread, writes notification event and be set to non-toggle state simultaneously; When lock interface is write in release, enter and read critical section, write notification event simultaneously and be set to trigger state;
3) travel through described 2-1) to 2-2) return Read-Write Locks information complete Read-Write Locks arrange, realize resource sharing.
2. read-write lock implement method as claimed in claim 1, is characterized in that, define in described Read-Write Locks assembly: open member's constructed fuction CReadWriteLock and destructor function ~ CReadWriteLock; Application read lock interface function ReadLock and release read lock interface function ReadUnlock; Lock WriteLock interface function is write in application and lock interface function WriteUnlock is write in release.
3. read-write lock implement method as claimed in claim 1 or 2, is characterized in that, also define four privately owned members in described Read-Write Locks assembly: maximum read lock number m_ulMaxReadCount; Read critical section m_hReadCriticalSection; Write critical section m_hWriteCriticalSection; Write notification event m_hWriteNotifyEvent.
4. read-write lock implement method as claimed in claim 3, it is characterized in that, described maximum read lock number m_ulMaxReadCount is defaulted as 5.
5. read-write lock implement method as claimed in claim 1, is characterized in that, described step 2-2) in application when writing lock, the waiting-timeout time that a unit is Millisecond is set, obtains yet after exceeding the described stand-by period and write lock and namely return failure.
6. read-write lock implement method as claimed in claim 1, is characterized in that, described step 2-1) in, stop the method for operating of write or read thread as follows according to read lock number:
After 2-1-1) entering described two critical sections, if find, current read lock number reaches maximum number, then return failure, otherwise illustrates that current thread can proceed read lock operation;
2-1-2) as described step 2-1-1) in current read lock number do not reach maximum number, current read lock number can be increased, if increase after read lock number be greater than 0, then can not allow to write thread and operate.
7. read-write lock implement method as claimed in claim 1, is characterized in that, described step 2-2) in, stop the method for the operation of write or read thread as follows according to writing lock number:
2-2-1) be introduced into and write critical section and stop other thread application read lock or write lock, then within the stand-by period of setting etc. notification event to be written to guarantee not having other thread to carry out read-write operation to shared resource;
If 2-2-2) write notification event within the stand-by period of described setting not to be triggered, then wait timeout, returns failure; If write notification event to be at the appointed time triggered, then illustrate and now do not read thread, what acquisition was exclusive writes lock, then will write notification event and be set to non-toggle state, and be latched in as long as have read lock or write, and does not just allow new writing to lock application, returns successfully;
2-2-3) described step 2-2-2) if return successfully, application is write locking wire journey and can not be left and write critical section, when returning failure, the thread that lock is write in application will leave at once writes critical section.
8. read-write lock implement method as claimed in claim 1, is characterized in that,
In described release read lock process, only enter and read critical section, do not enter and write critical section, then read lock number is subtracted 1, when reducing to 0, notification event being set to trigger state, exiting and reading critical section;
Described release is write in lock process, first will write notification event and be set to trigger state, then exits and write critical section.
9. read-write lock implement method as claimed in claim 1, it is characterized in that, described Read-Write Locks does not have to operate in the operating system of built-in Read-Write Locks in Windows 2000, WindowsXP and Windows 2003Server.
CN201210511370.2A 2012-12-03 2012-12-03 A kind of read-write lock implement method Active CN102999378B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201210511370.2A CN102999378B (en) 2012-12-03 2012-12-03 A kind of read-write lock implement method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201210511370.2A CN102999378B (en) 2012-12-03 2012-12-03 A kind of read-write lock implement method

Publications (2)

Publication Number Publication Date
CN102999378A CN102999378A (en) 2013-03-27
CN102999378B true CN102999378B (en) 2015-08-26

Family

ID=47927982

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201210511370.2A Active CN102999378B (en) 2012-12-03 2012-12-03 A kind of read-write lock implement method

Country Status (1)

Country Link
CN (1) CN102999378B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106980544A (en) * 2017-03-31 2017-07-25 北京奇艺世纪科技有限公司 A kind of thread synchronization method and thread synchronization system

Families Citing this family (24)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113835901A (en) * 2013-10-15 2021-12-24 北京奥星贝斯科技有限公司 Read lock operation method, write lock operation method and system
CN104268024B (en) * 2014-09-24 2018-02-23 浪潮(北京)电子信息产业有限公司 A kind of method and apparatus for realizing process exclusive
US20160092488A1 (en) * 2014-09-26 2016-03-31 Futurewei Technologies, Inc. Concurrency control in a shared storage architecture supporting on-page implicit locks
CN104461707B (en) * 2014-11-28 2018-09-28 华为技术有限公司 a kind of lock request processing method and device
CN106681658A (en) * 2016-11-25 2017-05-17 天津津航计算技术研究所 Method for achieving high-speed transfer of mass data of data recorder on basis of multithreading
CN106681836B (en) * 2016-12-28 2021-03-05 华为技术有限公司 Semaphore creation method and semaphore creation device
CN106790694B (en) * 2017-02-21 2020-04-14 广州爱九游信息技术有限公司 Distributed system and scheduling method of target object in distributed system
CN106874098A (en) * 2017-03-02 2017-06-20 上海时年信息科技有限公司 Based on high concurrent critical resource processing method and system that redis is realized
CN107632794A (en) * 2017-10-20 2018-01-26 北京小米移动软件有限公司 Read-Write Locks control method and device
CN108572876B (en) * 2018-03-07 2020-11-20 北京神州绿盟信息安全科技股份有限公司 Method and device for realizing read-write lock
CN108768884A (en) * 2018-04-27 2018-11-06 江苏中科芯核电子科技有限公司 A kind of wireless communication data processing method based on buffer circle
CN109274774A (en) * 2018-11-16 2019-01-25 郑州云海信息技术有限公司 A kind of date storage method, device and computer readable storage medium
CN111857545B (en) * 2019-04-28 2023-12-29 深信服科技股份有限公司 Read-write lock conflict judging method, system, equipment and computer medium
CN110221923B (en) * 2019-06-06 2021-06-08 北京字节跳动网络技术有限公司 Data access method, device and equipment
CN110321357B (en) * 2019-06-14 2021-06-29 北京奇艺世纪科技有限公司 Read-write control method and device and server
CN110490581B (en) * 2019-07-18 2022-09-30 拉货宝网络科技有限责任公司 Distributed system critical data resource updating method and system
CN111143080A (en) * 2019-12-31 2020-05-12 联想(北京)有限公司 Read-write lock optimization method, device and system and storage medium
CN111913810B (en) * 2020-07-28 2024-03-19 阿波罗智能技术(北京)有限公司 Task execution method, device, equipment and storage medium in multithreading scene
CN111880940A (en) * 2020-07-29 2020-11-03 北京浪潮数据技术有限公司 Data processing method, device, equipment and storage medium in storage system
CN112559210B (en) * 2020-12-16 2024-05-07 北京仿真中心 Shared resource read-write mutual exclusion method based on RTX real-time system
CN112969092B (en) * 2021-01-29 2022-05-10 稿定(厦门)科技有限公司 Video file playing system
CN115202884B (en) * 2022-07-26 2023-08-22 江苏安超云软件有限公司 Method for adding read write lock of high-performance system based on polling and application
CN115543970B (en) * 2022-11-29 2023-03-03 本原数据(北京)信息技术有限公司 Data page processing method, data page processing device, electronic equipment and storage medium
CN117667316A (en) * 2023-12-05 2024-03-08 上海浦东发展银行股份有限公司 Asynchronous multithreading file processing method, device and storage medium

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1952898A (en) * 2005-03-14 2007-04-25 Qnx软件操作系统公司 Adaptive partitioning process scheduler
US8135690B2 (en) * 2009-01-05 2012-03-13 Microsoft Corporation Concurrency object classification

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8495638B2 (en) * 2010-09-08 2013-07-23 International Business Machines Corporation Component-specific disclaimable locks

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1952898A (en) * 2005-03-14 2007-04-25 Qnx软件操作系统公司 Adaptive partitioning process scheduler
US8135690B2 (en) * 2009-01-05 2012-03-13 Microsoft Corporation Concurrency object classification

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106980544A (en) * 2017-03-31 2017-07-25 北京奇艺世纪科技有限公司 A kind of thread synchronization method and thread synchronization system
CN106980544B (en) * 2017-03-31 2020-03-03 北京奇艺世纪科技有限公司 Thread synchronization method and thread synchronization system

Also Published As

Publication number Publication date
CN102999378A (en) 2013-03-27

Similar Documents

Publication Publication Date Title
CN102999378B (en) A kind of read-write lock implement method
CN102566979B (en) Method, device and multi-core processor system for realizing self-adaptive lock
CN101203831B (en) Device, method and system for caching memory update
CN100573456C (en) A kind of paralleling multi-processor virtual machine system
US8316190B2 (en) Computer architecture and method of operation for multi-computer distributed processing having redundant array of independent systems with replicated memory and code striping
CN101566977B (en) Method, device and system of processor accessing shared data
US7526673B2 (en) Parallel processing system by OS for single processors and parallel processing program
EP0735473A2 (en) Method and apparatus for managing a database in a distributed object operating environment
US20130047011A1 (en) System and Method for Enabling Turbo Mode in a Processor
CN103473129B (en) Multi-task queue scheduling system with scalable number of threads and implementation method thereof
CN101599027A (en) A kind of thread pool management method and system thereof
KR20110071018A (en) Method for the deterministic execution and synchronisation of an information processing system comprising a plurality of processing cores executing system tasks
DE112012002465T5 (en) Graphics processor with non-blocking concurrent architecture
WO2005078575A2 (en) Sharing idled processor execution resources
CN101876926A (en) Asymmetric software triple-computer hot backup fault-tolerant method
CN105700941A (en) Three-dimensional scheduler model and scheduling algorithm thereof
CN102929711A (en) Implementing method of real-time transactional memory of software
CN114756170B (en) Storage isolation system and method for container application
CN101556597A (en) Self-adaptive optimistic concurrency control method
Chen et al. Loop-free asynchronous data sharing in multiprocessor real-time systems based on timing properties
US20190073243A1 (en) User-space spinlock efficiency using c-state and turbo boost
Molesky et al. Implementing a predictable real-time multiprocessor kernel-the Spring kernel
CN101539864B (en) Method for self adaptedly safeguarding the normal starting of credible client virtual domain
CN103257892A (en) Multi-task scheduling method and system based on macro assembly
US8799903B1 (en) Systems and methods for exchanging runtime functionalities between software stacks

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant