CN102999378A - Read-write lock implement method - Google Patents
Read-write lock implement method Download PDFInfo
- Publication number
- CN102999378A CN102999378A CN2012105113702A CN201210511370A CN102999378A CN 102999378 A CN102999378 A CN 102999378A CN 2012105113702 A CN2012105113702 A CN 2012105113702A CN 201210511370 A CN201210511370 A CN 201210511370A CN 102999378 A CN102999378 A CN 102999378A
- 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.)
- Granted
Links
Images
Landscapes
- Storage Device Security (AREA)
Abstract
The invention relates to a method for implementing synchronization among threads through a read-write lock. The method comprises the steps of: (1) initializing the read-write lock, initializing a read critical section and a write critical section to be at an enterable state and initializing a write notification event to be at a triggered state; (2) protecting critical codes in a shared resource according to the critical sections when a plurality of read threads and/or write threads concurrently access the shared resource, and setting the read-write lock; and (3) returning read-write lock information, and finishing the read-write lock setting to realize resource sharing. The method provided by the invention can implement the read-write lock by using the combination of the critical sections and the events, and has the advantages of being simple and easy to operate, light in load and high in speed. Meanwhile, the method can avoid the cost on frequently switching user states when a kernel mode object is used as the critical sections are taken as a main mutex tool; and the write threads do not need to wait infinitely and can access the shared resource after all of the read threads complete the operations when the lock write is applied, so as to prevent the condition that subsequent threads used for applying the read lock from occupies the shared resource, thereby greatly improving the performance of the read-write lock.
Description
Technical field
The present invention relates to the Read-Write Locks implementation method, relate in particular to the method for synchronous based on the cross-thread of Read-Write Locks, can be applied to the software development technique field of various software systems research and development, belong to areas of information technology
Background technology
In areas of information technology, software development relates to a lot of technology, comprise internal storage data organization and administration, message communicating, index technology, synchronization and mutex technology etc., the present invention pays close attention to the simultaneous techniques between the software development thread, in some specific software systems R﹠D processes, relate to the simultaneous techniques of cross-thread, and the requirement that synchronous operation is had higher real-time and high efficiency, such as: rdal-time DBMS, relational database management system, middleware etc.
Existing concurrent synchronization and mutex object (can see official of Microsoft for details about the illustrative material http://msdn.microsoft.com/en-us/library/windows/desktop/ms68426 6%28v=vs.85%29.as px of Mutex object) mainly contains mutex (Mutex), semaphore (Semaphore), event (Event), critical section (Critical Section) (can see official of Microsoft for details about the illustrative material http://msdn.microsoft.com/en-us/library/windows/desktop/ms68253 0%28v=vs.85%29.as px of critical section object) etc., can be divided into kernel state object and user's attitude object according to level under the object, also can be divided into inter process synchronization technology and cross-thread simultaneous techniques according to purposes.The critical section is the easy way that guarantees at a time to only have a thread energy visit data, is user's attitude object; Mutex is similar with the critical section, and the thread that only has Mutex object just has the authority of access resources, but more complicated than the critical section, belongs to the kernel state object, can realize the safety of resource is shared between the different application process.Semaphore is used for control to have the limited quantity user resources to one and carries out Concurrency Access, comprise a usage count, maximum resource number, current resource count, the maximum resource number represents the maximum resource quantity that semaphore can be controlled, and current number of resources represents the current available resource quantity of signal; Event is used for notifying thread to have some event to occur, thereby wakes this thread execution subsequent tasks up.Therefore mutex, semaphore, event all are mutual exclusion or the simultaneous techniquess of kernel state, also can be used as process and cross-thread is synchronous.The critical section can only be synchronous as cross-thread, but owing to be user's attitude technology, the context switch overhead from kernel state to user's attitude not, so its performance is better than mutex.
Concurrent requests a large amount of in the software systems can be divided into a read request and read-write requests, and the former can operate simultaneously to shared resource, and the latter must the exclusive shared resource that occupies, and does not allow other people to access.It all is inappropriate directly using mutex, semaphore, event etc. in this case, the handover overhead that larger kernel state and user's attitude are arranged on the one hand, do not distinguish on the other hand the characteristics of concurrent reading and writing request, cause can concurrent execution a read request become serial and carry out, greatly reduce system effectiveness.Therefore, need to be for a large amount of concurrent reading and writing requests, well-designed a kind of cross-thread synchronization mechanism is realized maximized concurrent execution, thereby improves the overall performance of software systems.
Summary of the invention
The present invention is directed to synchronous real-time demand and the performance requirement of cross-thread of rdal-time DBMS, relational database management system, middleware, design and proposed a kind of method for synchronous of multi-thread access shared resource real-time.
The technical problem to be solved in the present invention
Usually can run into the problem of multi-thread concurrent access shared resource in the R ﹠ D Software Development systematic procedure, if concurrency is lower, (Mutex) just can realize with simple mutex.But when concurrency was higher, some high performance software systems also required the synchronous operation between multithreading to have higher performance.Concurrent multithreading can be divided into two classes according to whether revising shared resource: read-only thread and revise thread, a good multithreading simultaneous techniques should allow a plurality of read-only threads can access simultaneously shared resource, secondly more new thread should can be obtained shared resource at once after waiting for current read-only thread complete operation, follow-up read-only thread is seized shared resource from revising thread situation can not occur.Because mainstream operation system such as Windows 2000, Windows XP and Windows 2003 Server do not have built-in Read-Write Locks, therefore usually need the developer to expend the Read-Write Locks of long time Design and implementation cross-thread, and the final effect that realizes might not satisfy the software systems high performance requirements synchronous to cross-thread.
The present invention is directed to this class technical matters, the Read-Write Locks solution that has proposed a kind of cross-thread general, real-time is as follows, has provided the specific implementation details of Read-Write Locks.On the one hand, developer's time can be greatly reduced, on the other hand, software systems can be satisfied to carrying out synchronous performance requirement between a plurality of read-write threads.
A kind of Read-Write Locks implementation method, its step comprises:
1) initialization Read-Write Locks is read the critical section, writes the critical section and is write three objects of notification event according to the Read-Write Locks building component, describedly reads the critical section and the described critical section of writing is initialized as and can gets the hang of, and the described notification event of writing is initialized as the state of being triggered;
2) read thread and/or write thread Concurrency Access shared resource when a plurality of, protect key code in the described shared resource according to described critical section, Read-Write Locks is set;
2-1) during application read lock interface, be introduced into and write the critical section and enter and read the critical section, stop the operation of write or read thread according to the read lock number, the described notification event of writing is made as non-triggering state simultaneously; During the release read lock interface, enter and read the critical section, if the read lock number was 0 and simultaneously the described notification event of writing was made as the triggering state this moment;
2-2) application is when writing lock interface, is introduced into to write the critical section and enter and read the critical section, and according to the operation of writing the lock number and stop the write or read thread, the described notification event of writing is made as non-triggering state simultaneously; When lock interface is write in release; Enter and read the critical section, the described notification event of writing is made as the triggering state simultaneously;
3) travel through described 2-1) to 2-2) return Read-Write Locks information and finish the Read-Write Locks setting, realize resource sharing.
Define in the 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 have also been defined in the 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.
Described maximum read lock number m_ulMaxReadCount is defaulted as 5.
When lock was write in application described step 2-2), it was the waiting-timeout time of Millisecond that a unit is set, and does not obtain yet to write lock and namely return failure after surpassing the described stand-by period.
Further, described step 2-1) in, stop the method for operating of write or read thread as follows according to the read lock number:
2-1-1) enter described two critical sections after, reached maximum number if find current read lock number, then return failure, otherwise the explanation current thread can be proceeded read lock operation;
2-1-2) as described step 2-1-1) in current read lock number do not reach maximum number, can increase current read lock number, if the read lock number is 1 after increasing, then can not allows to write thread and operate.
Further, described step 2-2) in, as follows according to the method for the operation of writing lock number prevention write or read thread:
2-2-1) be introduced into and write the 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 that shared resource is carried out read-write operation;
Be not triggered if 2-2-2) write notification event within the stand-by period of described setting, then wait timeout returns failure; Be triggered if write notification event at the appointed time, then do not read explanation at this moment thread, obtain the lock of writing monopolized, then write notification event and be set to non-triggering state, be latched in as long as read lock is arranged or write, just do not allow the new lock application of writing, return successfully;
2-2-3) described step 2-2-2) if return successfully, application is write the locking wire journey and can not left and write the critical section, and when returning failure, the thread that lock is write in application will leave at once writes the critical section.
Further, in the described release read lock process, only enter and read the critical section, do not enter and write the critical section, then the read lock number is subtracted 1, when reducing to 0, notification event is made as the triggering state, withdraw from and read the critical section;
Further, described release is write in the lock process, will write first notification event and be made as the triggering state, withdraws from and writes the critical section.
Further, described Read-Write Locks does not have to operate in the operating system of built-in Read-Write Locks in Windows2000, WindowsXP and Windows2003Server.
The Read-Write Locks solution relates generally to three parts, and Read-Write Locks is carried out concurrent request, parallel processing: 1) Read-Write Locks initialization procedure; 2) application and release read lock; 3) application is write lock with release.This three part is included among the Read-Write Locks class CReadWriteLock of the present invention's realization; give outside the use as the open member function of class; two critical section objects of inner employing are protected the reading and writing key code respectively, and the use case object is notified the state of writing lock.Because the speed of critical section far away faster than mutex, therefore adopts the critical section amount can obtain higher operational efficiency.Class CReadWriteLock uses inside a current read lock number of variable record, is applying for, is discharging the relation that all will judge when locking with maximum read lock number, thereby determining the flow process that application is locked.
In the Read-Write Locks initialization procedure, establishment and initialization are read the critical section, write the critical section and are write three objects of notification event respectively, the 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, thereby can accept to write the lock application, then the current read lock number of initialization is 0, namely also can receive the read lock application.
In application read lock process, need to read and revise current read lock number, therefore protect this section code with the critical section; the critical section is divided into two, and one is to write the critical section, is used for protecting the operation of writing phase-locked pass; one is to read the critical section, is used for protecting the relevant operation of read lock.Because it is exclusive writing lock, therefore must be introduced into and write the critical section, then enter and read the critical section, the order when withdrawing from is then opposite, withdraws from first and reads the critical section, withdraws from and writes the critical section, so just can avoid occurring the situation of deadlock.After entering two critical sections, reached maximum number if find current read lock number, then return failure, otherwise illustrate that current thread can also obtain read lock, so increase current read lock number, if the read lock number is 1 after increasing, then can not allows to write thread and operate, can also continue to increase the read lock number this moment, as long as be no more than the maximum read lock number of setting.When being arranged, read lock just do not allow application to write lock.So also will arrange and write notification event is non-triggering state, stop other thread to obtain to write lock.
In discharging the read lock process, because this moment, the read lock number was greater than 0, can not there be thread to obtain to write lock, therefore only need to enter and read the critical section, write the critical section and need not to enter, then the read lock number is subtracted 1, explanation does not have thread that shared resource has been carried out read operation this moment when reducing to 0, therefore can allow thread to carry out write operation, writing notification event by setting is that the triggering state wakes the thread of writing of waiting for up.
Write in the lock process in application, to enter first and write the critical section and stop other thread application read lock or write lock, namely guarantee can not seized by the thread of follow-up application Read-Write Locks, then wait notification event to be written to guarantee not having other thread that shared resource is carried out read-write operation, this waiting process can arrange time-out time, writes the success ratio of lock to improve application, is not triggered if write notification event at the appointed time, then wait timeout returns failure.If writing notification event at the appointed time is triggered, then do not read explanation at this moment thread, the applicant can obtain to monopolize writes lock, then writes notification event and is set to non-triggering state, be latched in as long as return read lock successfully to be arranged or write, just do not allow new writing to lock application.Return successfully.If return successfully, in order to ensure the exclusivity of writing lock, application is write the locking wire journey and can not left and write the critical section, so that the thread of other application Read-Write Locks is write this step of critical section and just got clogged entering; When returning unsuccessfully, the thread that lock is write in application will leave at once writes the critical section.
Write in the lock process in release, write the critical section but do not withdraw owing to just entered during read lock in application, therefore when lock is write in release, just do not need to enter and do not write the critical section, as long as will write first notification event is made as the triggering state, then withdraws from and write the critical section and get final product.
Beneficial effect of the present invention
Can find out that according to above technical solution the present invention has following beneficial effect:
1, the present invention proposes the method that realizes Read-Write Locks with critical section and composition of matter, have fast characteristics of the usefulness of being simple and easy to, light weight.This method employing critical section is as main mutual exclusion instrument, and the User Status when having avoided using the kernel state object is the expense of switching frequently, has greatly improved the performance of Read-Write Locks.
2, the present invention has done following optimization for the scene of reading in a large number thread and writing on a small quantity thread: read thread and read not conflict between the thread, and apply for that again the expense of read lock is very little when having read lock; Write thread and can infinitely not wait for, only need to wait by the time application is read all complete operations of thread when writing lock, and can not applied for that the thread of read lock seizes shared resource by the back.
3, the invention provides a Read-Write Locks and realize class, have succinct application lock, discharge the calling interface of locking, can be integrated into very easily in the various software systems.
Description of drawings
Fig. 1 is the process flow diagram of application read lock among Read-Write Locks implementation method one embodiment of the present invention;
Fig. 2 is the process flow diagram that discharges read lock among Read-Write Locks implementation method one embodiment of the present invention;
Fig. 3 is the process flow diagram that lock is write in application among Read-Write Locks implementation method one embodiment of the present invention;
Fig. 4 discharges the process flow diagram of writing lock among Read-Write Locks implementation method one embodiment of the present invention;
Fig. 5 is the schematic flow sheet of Read-Write Locks implementation method of the present invention.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the invention, the technical scheme in the embodiment of the invention is clearly and completely described, be understandable that described embodiment only is the present invention's part embodiment, rather than whole embodiment.Based on the embodiment among the present invention, those skilled in the art belong to the scope of protection of the invention not making the every other embodiment that obtains under the creative work prerequisite.
Be the schematic flow sheet of Read-Write Locks implementation method of the present invention as shown in Figure 5, the Read-Write Locks solution relates generally to three parts, and Read-Write Locks is carried out concurrent request, parallel processing: 1) Read-Write Locks initialization procedure; 2) application and release read lock; 3) application is write lock with release.
The Read-Write Locks assembly
The Read-Write Locks class definition is as follows:
[1]classCReadWriteLock{
[2]public:
[3]CReadWriteLock(ULONGulMaxReadCount=5);
[4]virtual~CReadWriteLock();
[5]virtualBOOLReadLock();
[6]virtualvoidReadUnLock();
[7]virtualBOOLWriteLock(ULONGulWaitMSTime=INFINITE);
[8]virtualvoidWriteUnLock();
[9]protected:
[10]ULONGm_ulMaxReadCount;
[11]CRITICAL_SECTIONm_hReadCriticalSection;
[12]CRITICAL_SECTIONm_hWriteCriticalSection;
[13]HANDLEm_hWriteNotifyEvent;
[14]};
Wherein defined six public method: CReadWriteLock, ~ CReadWriteLock is constructed fuction and destructor function; ReadLock, ReadUnlock are application read lock and the interface function that discharges read lock; WriteLock, WriteUnlock are that application is write lock and discharged the interface function of writing lock.
Also define in addition four privately owned member: m_ulMaxReadCount and represented maximum read lock number, be defaulted as 5; M_hReadCriticalSection is the critical section that read operation is protected, and namely reads the critical section, and m_hWriteCriticalSection is the critical section that write operation is protected, and namely writes the critical section; M_hWriteNotifyEvent writes notification event, is used to indicate the state of writing lock.
The initialization interface method realizes
The constructed fuction of class CReadWriteLock is the initialization interface method, is defined as:
CReadWriteLock::CReadWriteLock(ULONGulMaxReadCount=5);
Wherein parameters u lMaxReadCount is used for the maximum concurrent read lock number that Read-Write Locks allows is set, 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 object reference system interfaces such as the critical section that the method is mainly used Read-Write Locks, event carry out initialization, write notification event and are initialized to the triggering state, and maximum concurrent read lock number is set simultaneously.
The process flow diagram of application read lock among Read-Write Locks implementation method one embodiment of the present invention as shown in Figure 1; Rreturn value is set to TRUE first, enters to write the critical section, enter two critical sections after, begin the read lock number and write notification event to operate, arrived maximum number if then judge the read lock number, then rreturn value is set to FALSE, otherwise the read lock number is increased by 1.Then judge whether the read lock number is 1, if it is explanation has just had thread application read lock, and therefore should refuse other thread application writes lock, and therefore resetting and writing notification event is non-triggering state; After finishing aforesaid operations, leave the critical section; At last the rreturn value of setting is returned to caller.
Below be application read lock interface method specific definition during application read lock interface method is realized:
BOOLCReadWriteLock::ReadLock();
If TRUE is then returned in the success of application read lock; If FALSE is then returned in the failure of application read lock.
Application read lock interface method specific implementation code is as follows:
[1]BOOLbSucc=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]returnbSucc;
Wherein, the 1st row first rreturn value be set to TRUE; The 2nd is advanced into and writes the critical section, and the 3rd advances studies in the critical section; After entering two critical sections, begin the read lock number and write notification event to operate, if the 4th row is judged the read lock number and has been arrived maximum number, then be about to rreturn value the 5th and be set to FALSE, increase by 1 otherwise the 7th is about to the read lock number, then eighth row judges whether the read lock number is 1, and if it is illustrating has just had thread application read lock, therefore should refuse other thread application writes lock, and therefore writing notification event in the replacement of the 9th row is non-triggering state; After finishing aforesaid operations, leave the critical section at the 11st, 12 row; At last the rreturn value of setting is returned to caller.
Shown in Figure 2 is the process flow diagram that discharges read lock among Read-Write Locks implementation method one embodiment of the present invention, and application enters reads the critical section; After entering the critical section, the read lock number is subtracted 1; Then judge the read lock number, if equal 0 then explanation this moment has not had read lock, arranging and writing notification event is the triggering state, can obtain to write lock so that applying for the thread of writing lock; Leave and read the critical section.
Below be release read lock interface method specific definition during release read lock interface method realizes:
voidCReadWriteLock::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_hWriteNotityEvent);
[5]::LeaveCriticalSection(&m_hReadCriticalSection);
Wherein the 1st row application enters and reads the critical section; After entering the critical section, the 2nd row then subtracts 1 with the read lock number; The 3rd row is judged the read lock number, if equal 0 then explanation has not had read lock this moment, therefore writing notification event in the 4th row setting is the triggering state, can obtain to write lock so that applying for the thread of writing lock; The 5th row is to leave to read the critical section.
Shown in Figure 3 is the process flow diagram that lock is write in application among Read-Write Locks implementation method one embodiment of the present invention, and wherein, application enters writes the critical section; Enter write the critical section after, etc. notification event to be written and be provided with the wait timeout time, if writing notification event at the appointed time is triggered then successfully returns, do not withdraw from the critical section this moment, this is to monopolize because of write operation, therefore do not allow other thread to have simultaneously and write lock, the thread of other application Read-Write Locks is stoped entering and write this initial step of critical section; Be not triggered if write notification event at the appointed time, read thread in this means during this period of time not finish all operations, therefore can not carry out write operation, write the critical section so withdraw from, so that then follow-up thread application Read-Write Locks returns FALSE.
Below be that the specific definition that the lock interface method realizes applying for writing the lock interface method is write in application:
BOOLCReadWriteLock::WriteLock(ULONGulWaitMSTime);
Wherein parameters u lWaitMSTime is the stand-by period that lock is write in application, and unit is millisecond, still fails to apply for successfully if surpass this time, shows and this time applies for the operation overtime failure, returns FALSE; If apply for successfully at the appointed time, then return TRUE.
It is as follows that the specific implementation code of lock interface method is write in application:
[1]::EnterCriticalSection(&m_hWriteCriticalSection);
[2]if(WAIT_OBJECT_0==::WaitForSingleObject(m_hWriteNotifyEvent,ulWaitMSTime))
[3]returnTRUE;
[4]else{
[5]::LeaveCriticalSection(&m_hWriteCriticalSection);
[6]returnFALSE;
[7]}
Wherein, the 1st row application enters and writes the critical section; Enter write the critical section after, at notification events to be written such as the 2nd row and be provided with the wait timeout time, if write at the appointed time that notification event is triggered then successfully return at the 3rd row, do not withdraw from the critical section this moment, this is to monopolize because of write operation, therefore do not allow other thread to have simultaneously and write lock, the thread of other application Read-Write Locks is stoped entering and write this initial step of critical section; If writing notification event at the appointed time is not triggered, therefore read thread in this means during this period of time and do not finish all operations, can not carry out write operation, write the critical section so withdraw from the 5th row, so that then follow-up thread application Read-Write Locks returns FALSE at the 6th row.
Shown in Figure 4 is to discharge the process flow diagram of writing lock among Read-Write Locks implementation method one embodiment of the present invention, and resetting and writing notification event is the triggering state, then withdraws from the critical section of protection write operation, so that follow-up thread application Read-Write Locks.
Below be to discharge to write the specific definition that the lock interface method is write in lock interface method realization release:
voidCReadWriteLock::WriteUnLock();
The method inside is relatively simple, does not define rreturn value.
It is as follows that the specific code of lock interface method is write in release:
[1]::SetEvent(m_hWriteNotifyEvent);
[2]::LeaveCriticalSection(&m_hWriteCriticalSection);
Wherein the 1st row is to reset that to write notification event be the triggering state, and then the 2nd row withdraws from the critical section of protection write operation, so that follow-up thread application Read-Write Locks.
Claims (9)
1. Read-Write Locks implementation method, its step comprises:
1) initialization Read-Write Locks is read the critical section, writes the critical section and is write three objects of notification event according to the Read-Write Locks building component, describedly reads the critical section and the described critical section of writing is initialized as and can gets the hang of, and the described notification event of writing is initialized as the state of being triggered;
2) read thread and/or write thread Concurrency Access shared resource when a plurality of, protect key code in the described shared resource according to described critical section, Read-Write Locks is set;
2-1) during application read lock interface, be introduced into and write the critical section and enter and read the critical section, stop the operation of write or read thread according to the read lock number, the described notification event of writing is made as non-triggering state simultaneously; During the release read lock interface, enter and read the critical section, simultaneously the described notification event of writing is made as the triggering state if the read lock number is 0;
2-2) application is when writing lock interface, is introduced into to write the critical section and enter and read the critical section, and according to the operation of writing the lock number and stop the write or read thread, the described notification event of writing is made as non-triggering state simultaneously; When lock interface is write in release, enter and read the critical section, the described notification event of writing is made as the triggering state simultaneously;
3) travel through described 2-1) to 2-2) return Read-Write Locks information and finish the Read-Write Locks setting, realize resource sharing.
2. Read-Write Locks implementation method as claimed in claim 1 is characterized in that, defines in the 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 Locks implementation method as claimed in claim 1 or 2 is characterized in that, has also defined four privately owned members in the 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 Locks implementation method as claimed in claim 3 is characterized in that, described maximum read lock number m_ulMaxReadCount is defaulted as 5.
5. Read-Write Locks implementation method as claimed in claim 1 is characterized in that, described step 2-2) when lock was write in middle application, it was the waiting-timeout time of Millisecond that a unit is set, after surpassing the described stand-by period, obtain yet to write lock and namely returning failure.
6. Read-Write Locks implementation 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 the read lock number:
2-1-1) enter described two critical sections after, reached maximum number if find current read lock number, then return failure, otherwise the explanation current thread can be proceeded read lock operation;
2-1-2) as described step 2-1-1) in current read lock number do not reach maximum number, can increase current read lock number, if the read lock number is 1 after increasing, then can not allows to write thread and operate.
7. Read-Write Locks implementation method as claimed in claim 1 is characterized in that, described step 2-2) in, as follows according to the method for the operation of writing lock number prevention write or read thread:
2-2-1) be introduced into and write the 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 that shared resource is carried out read-write operation;
Be not triggered if 2-2-2) write notification event within the stand-by period of described setting, then wait timeout returns failure; Be triggered if write notification event at the appointed time, then do not read explanation at this moment thread, obtain the lock of writing monopolized, then write notification event and be set to non-triggering state, be latched in as long as read lock is arranged or write, just do not allow the new lock application of writing, return successfully;
2-2-3) described step 2-2-2) if return successfully, application is write the locking wire journey and can not left and write the critical section, and when returning failure, the thread that lock is write in application will leave at once writes the critical section.
8. Read-Write Locks implementation method as claimed in claim 1 is characterized in that,
In the described release read lock process, only enter and read the critical section, do not enter and write the critical section, then the read lock number is subtracted 1, when reducing to 0, notification event is made as the triggering state, withdraw from and read the critical section;
Described release is write in the lock process, will write first notification event and be made as the triggering state, withdraws from and writes the critical section.
9. Read-Write Locks implementation method as claimed in claim 1 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, Windows XP and Windows 2003 Server.
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 true CN102999378A (en) | 2013-03-27 |
CN102999378B 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 (24)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104268024A (en) * | 2014-09-24 | 2015-01-07 | 浪潮(北京)电子信息产业有限公司 | Method and device for achieving process mutual exclusion |
CN104461707A (en) * | 2014-11-28 | 2015-03-25 | 华为技术有限公司 | Locking request processing method and device |
CN104572568A (en) * | 2013-10-15 | 2015-04-29 | 阿里巴巴集团控股有限公司 | Read lock operation method, write lock operation method and system |
CN106575238A (en) * | 2014-09-26 | 2017-04-19 | 华为技术有限公司 | Concurrency control in shared storage architecture supporting on-page implicit locks |
CN106681658A (en) * | 2016-11-25 | 2017-05-17 | 天津津航计算技术研究所 | Method for achieving high-speed transfer of mass data of data recorder on basis of multithreading |
CN106681836A (en) * | 2016-12-28 | 2017-05-17 | 华为技术有限公司 | Creating method and device of signal quantity |
CN106790694A (en) * | 2017-02-21 | 2017-05-31 | 广州爱九游信息技术有限公司 | The dispatching method of destination object in distributed system and 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 |
CN108572876A (en) * | 2018-03-07 | 2018-09-25 | 北京神州绿盟信息安全科技股份有限公司 | A kind of implementation method and device of Read-Write Locks |
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 |
CN110221923A (en) * | 2019-06-06 | 2019-09-10 | 北京字节跳动网络技术有限公司 | Data access method, device and equipment |
CN110321357A (en) * | 2019-06-14 | 2019-10-11 | 北京奇艺世纪科技有限公司 | A kind of read/writing control method, device and server |
CN110490581A (en) * | 2019-07-18 | 2019-11-22 | 拉货宝网络科技有限责任公司 | A kind of distributed system critical data resource regeneration method and system |
CN111143080A (en) * | 2019-12-31 | 2020-05-12 | 联想(北京)有限公司 | Read-write lock optimization method, device and system and storage medium |
CN111857545A (en) * | 2019-04-28 | 2020-10-30 | 深信服科技股份有限公司 | Method, system, equipment and computer medium for judging conflict of read-write lock |
CN111880940A (en) * | 2020-07-29 | 2020-11-03 | 北京浪潮数据技术有限公司 | Data processing method, device, equipment and storage medium in storage system |
CN111913810A (en) * | 2020-07-28 | 2020-11-10 | 北京百度网讯科技有限公司 | Task execution method, device, equipment and storage medium under multi-thread scene |
CN112559210A (en) * | 2020-12-16 | 2021-03-26 | 北京仿真中心 | Shared resource read-write mutual exclusion method based on RTX real-time system |
CN112969092A (en) * | 2021-01-29 | 2021-06-15 | 稿定(厦门)科技有限公司 | Video file playing system |
CN115202884A (en) * | 2022-07-26 | 2022-10-18 | 江苏安超云软件有限公司 | Method for reading, reading and writing lock of high-performance system based on polling and application |
CN115543970A (en) * | 2022-11-29 | 2022-12-30 | 本原数据(北京)信息技术有限公司 | 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 |
Families Citing this family (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106980544B (en) * | 2017-03-31 | 2020-03-03 | 北京奇艺世纪科技有限公司 | Thread synchronization method and thread synchronization system |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1952898A (en) * | 2005-03-14 | 2007-04-25 | Qnx软件操作系统公司 | Adaptive partitioning process scheduler |
US20120060160A1 (en) * | 2010-09-08 | 2012-03-08 | International Business Machines Corporation | Component-specific disclaimable locks |
US8135690B2 (en) * | 2009-01-05 | 2012-03-13 | Microsoft Corporation | Concurrency object classification |
-
2012
- 2012-12-03 CN CN201210511370.2A patent/CN102999378B/en active Active
Patent Citations (3)
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 |
US20120060160A1 (en) * | 2010-09-08 | 2012-03-08 | International Business Machines Corporation | Component-specific disclaimable locks |
Cited By (36)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN104572568A (en) * | 2013-10-15 | 2015-04-29 | 阿里巴巴集团控股有限公司 | Read lock operation method, write lock operation method and system |
CN104572568B (en) * | 2013-10-15 | 2021-07-23 | 蚂蚁金服(杭州)网络技术有限公司 | 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 |
CN104268024A (en) * | 2014-09-24 | 2015-01-07 | 浪潮(北京)电子信息产业有限公司 | Method and device for achieving process mutual exclusion |
CN106575238A (en) * | 2014-09-26 | 2017-04-19 | 华为技术有限公司 | Concurrency control in 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 |
CN104461707A (en) * | 2014-11-28 | 2015-03-25 | 华为技术有限公司 | Locking 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 |
CN106681836A (en) * | 2016-12-28 | 2017-05-17 | 华为技术有限公司 | Creating method and device of signal quantity |
CN106681836B (en) * | 2016-12-28 | 2021-03-05 | 华为技术有限公司 | Semaphore creation method and semaphore creation device |
CN106790694A (en) * | 2017-02-21 | 2017-05-31 | 广州爱九游信息技术有限公司 | The dispatching method of destination object in distributed system and distributed system |
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 |
CN108572876A (en) * | 2018-03-07 | 2018-09-25 | 北京神州绿盟信息安全科技股份有限公司 | A kind of implementation method and device of Read-Write Locks |
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 |
CN111857545A (en) * | 2019-04-28 | 2020-10-30 | 深信服科技股份有限公司 | Method, system, equipment and computer medium for judging conflict of read-write lock |
CN110221923A (en) * | 2019-06-06 | 2019-09-10 | 北京字节跳动网络技术有限公司 | Data access method, device and equipment |
CN110321357A (en) * | 2019-06-14 | 2019-10-11 | 北京奇艺世纪科技有限公司 | A kind of read/writing control method, device and server |
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 |
CN110490581A (en) * | 2019-07-18 | 2019-11-22 | 拉货宝网络科技有限责任公司 | A kind of distributed system critical data resource regeneration 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 |
CN111913810A (en) * | 2020-07-28 | 2020-11-10 | 北京百度网讯科技有限公司 | Task execution method, device, equipment and storage medium under multi-thread scene |
CN111880940A (en) * | 2020-07-29 | 2020-11-03 | 北京浪潮数据技术有限公司 | Data processing method, device, equipment and storage medium in storage system |
CN112559210A (en) * | 2020-12-16 | 2021-03-26 | 北京仿真中心 | Shared resource read-write mutual exclusion method based on RTX real-time system |
CN112559210B (en) * | 2020-12-16 | 2024-05-07 | 北京仿真中心 | Shared resource read-write mutual exclusion method based on RTX real-time system |
CN112969092A (en) * | 2021-01-29 | 2021-06-15 | 稿定(厦门)科技有限公司 | Video file playing system |
CN115202884A (en) * | 2022-07-26 | 2022-10-18 | 江苏安超云软件有限公司 | Method for reading, reading and writing lock of high-performance system based on polling and application |
CN115202884B (en) * | 2022-07-26 | 2023-08-22 | 江苏安超云软件有限公司 | Method for adding read write lock of high-performance system based on polling and application |
CN115543970A (en) * | 2022-11-29 | 2022-12-30 | 本原数据(北京)信息技术有限公司 | Data page processing method, data page processing device, electronic equipment and storage medium |
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 |
Also Published As
Publication number | Publication date |
---|---|
CN102999378B (en) | 2015-08-26 |
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 | |
US5940827A (en) | Methods and apparatus for managing a database in a distributed operating environment | |
CN101833475A (en) | Be used to strengthen the primitive that thread-level is inferred | |
US7526673B2 (en) | Parallel processing system by OS for single processors and parallel processing program | |
CN103761182A (en) | Method and device for deadlock detection | |
CN1983193A (en) | System and method for processing information | |
US20130047011A1 (en) | System and Method for Enabling Turbo Mode in a Processor | |
EP1716482A2 (en) | Sharing idled processor execution resources | |
CN101183315A (en) | Paralleling multi-processor virtual machine system | |
CN101187908A (en) | Single chip multi-processor shared data storage space access method | |
CN102216902A (en) | Method for the deterministic execution and synchronisation of an information processing system comprising a plurality of processing cores executing system tasks | |
CN101566977A (en) | Method, device and system of processor accessing shared data | |
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 | |
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 | |
CN102141932B (en) | Large critical region protection method | |
CN112540871B (en) | Method for realizing general register reservation and recovery | |
US5708808A (en) | Method and apparatus for concurrency with critical regions | |
CN100483415C (en) | Method and apparatus for data library locking and manipulation | |
CN108628676A (en) | A kind of memory management device and method towards multiple nucleus system | |
JP3862293B2 (en) | Information processing method and apparatus | |
Fukuoka et al. | An efficient inter-node communication system with lightweight-thread scheduling |
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 |