[go: up one dir, main page]

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

本发明涉及一种通过读写锁实现线程间的同步方法,其步骤包括:1)初始化读写锁,将读临界区和写临界区初始化为可进入状态,写通知事件初始化为被触发状态;2)当多个读线程和/或写线程并发访问共享资源,根据临界区保护所述共享资源内关键代码,设置读写锁;3)返回读写锁信息完成读写锁设置,实现资源共享。本发明提出了以临界区和事件组合实现读写锁的方法,具有简单易用、轻量快速的特点,同时本方法采用临界区作为主要互斥工具,避免了使用内核态对象时的用户状态频繁切换的开销,写线程不会无限等待,只需要等待到申请写锁时的读线程都完成操作,而不会被后面申请读锁的线程抢占共享资源,大大提高了读写锁的性能。

The invention relates to a method for synchronizing threads through a read-write lock. The steps include: 1) initializing the read-write lock, initializing the read critical area and the write critical area to an accessible state, and initializing the write notification event to a triggered state; 2) When multiple reading threads and/or writing threads access shared resources concurrently, protect the key codes in the shared resources according to the critical section, and set the read-write lock; 3) Return the read-write lock information to complete the read-write lock setting and realize resource sharing . The present invention proposes a method for realizing a read-write lock by combining a critical section and an event, which has the characteristics of being simple and easy to use, lightweight and fast. At the same time, the method uses the critical section as the main mutual exclusion tool, avoiding the user state when using the kernel mode object Frequent switching overhead, the writing thread will not wait indefinitely, only need to wait until the reading thread when applying for the writing lock has completed the operation, and will not be preempted by the thread applying for the reading lock later, which greatly improves the performance of the reading and writing lock.

Description

一种读写锁实现方法A method for implementing read-write lock

技术领域technical field

本发明涉及读写锁实现方法,尤其涉及基于读写锁的线程间的同步方法,可以应用于各种软件系统研发的软件开发技术领域,属于信息技术领域The invention relates to a read-write lock implementation method, in particular to a synchronization method between threads based on a read-write lock, which can be applied to the technical field of software development of various software system research and development, and belongs to the field of information technology

背景技术Background technique

在信息技术领域中,软件开发涉及很多的技术,包括内存数据组织管理、消息通信、索引技术、同步互斥技术等,本发明关注于软件开发中线程间的同步技术,在一些特定的软件系统研发过程中涉及到线程间的同步技术,而且对同步操作具有较高的实时性和高效性的要求,比如说:实时数据库管理系统、关系数据库管理系统、中间件等。In the field of information technology, software development involves many technologies, including memory data organization and management, message communication, index technology, synchronization and mutual exclusion technology, etc. The present invention focuses on the synchronization technology between threads in software development. In some specific software systems The research and development process involves synchronization technology between threads, and has high real-time and high-efficiency requirements for synchronization operations, such as: real-time database management systems, relational database management systems, middleware, etc.

现有的并发同步互斥对象(可详见微软官方关于互斥对象的说明材料http://msdn.microsoft.com/en-us/library/windows/desktop/ms684266%28v=vs.85%29.aspx)主要有互斥量(Mutex)、信号量(Semaphore)、事件(Event)、临界区(Critical Section)(可详见微软官方关于临界区对象的说明材料http://msdn.microsoft.com/en-us/library/windows/desktop/ms682530%28v=vs.85%29.aspx)等,可根据对象所属层次分为内核态对象和用户态对象,也可根据用途分为进程间同步技术和线程间同步技术。临界区是保证在某一时刻只有一个线程能访问数据的简便办法,是用户态对象;互斥量跟临界区相似,只有拥有互斥对象的线程才具有访问资源的权限,但是比临界区复杂,属于内核态对象,可以在不同应用进程之间实现对资源的安全共享。信号量用来控制对一个具有有限数量用户资源进行并发访问,包含一个使用计数、最大资源数、当前资源计数,最大资源数表示信号量可以控制的最大资源数量,当前资源数表示信号当前可用的资源数量;事件用来通知线程有某些事件已发生,从而唤醒该线程执行后继任务。互斥量、信号量、事件都是内核态的互斥或同步技术,因此也可用作进程和线程间同步。临界区只能用作线程间同步,但由于是用户态技术,没有从内核态到用户态的环境切换开销,因此其性能优于互斥量。Existing concurrent synchronous mutex objects (see Microsoft's official explanation materials about mutex objects http://msdn.microsoft.com/en-us/library/windows/desktop/ms684266%28v=vs.85%29 .aspx) mainly includes mutex (Mutex), semaphore (Semaphore), event (Event), critical section (Critical Section) (see Microsoft's official description material about critical section objects http://msdn.microsoft. com/en-us/library/windows/desktop/ms682530%28v=vs.85%29.aspx), etc., can be divided into kernel state objects and user state objects according to the level to which the object belongs, and can also be divided into inter-process synchronization according to the purpose technology and inter-thread synchronization technology. A critical section is a simple way to ensure that only one thread can access data at a certain time. It is a user-mode object; a mutex is similar to a critical section. Only threads with mutex objects have access to resources, but it is more complicated than a critical section. , which belongs to the kernel state object, can realize the safe sharing of resources among different application processes. The semaphore is used to control concurrent access to a resource with a limited number of users, including a usage count, the maximum number of resources, and the current resource count. The maximum number of resources indicates the maximum number of resources that the semaphore can control, and the current number of resources indicates the number of resources currently available to the signal. The number of resources; the event is used to notify the thread that certain events have occurred, thereby waking up the thread to perform subsequent tasks. Mutexes, semaphores, and events are all kernel-mode mutual exclusion or synchronization technologies, so they can also be used for inter-process and thread synchronization. The critical section can only be used for inter-thread synchronization, but because it is a user-mode technology, there is no environment switching overhead from kernel mode to user mode, so its performance is better than mutex.

软件系统中大量的并发请求可以分为只读请求和读写请求,前者可以对共享资源同时操作,后者必须排它性的占有共享资源,不允许他人访问。这种情况下直接使用互斥量、信号量、事件等都是不合适的,一方面有较大的内核态与用户态的切换开销,另一方面没有区分并发读写请求的特点,导致可以并发执行的只读请求变成串行执行,大大降低了系统效率。因此,需要针对大量并发读写请求,精心设计一种线程间同步机制,实现最大化的并发执行,从而提高软件系统的整体性能。A large number of concurrent requests in a software system can be divided into read-only requests and read-write requests. The former can operate on shared resources at the same time, while the latter must exclusively occupy shared resources and not allow others to access them. In this case, it is inappropriate to use mutexes, semaphores, events, etc. directly. On the one hand, there is a large switching overhead between the kernel state and the user state, and on the other hand, there is no distinction between concurrent read and write requests, resulting in possible Read-only requests that are executed concurrently become serial executions, greatly reducing system efficiency. Therefore, it is necessary to carefully design an inter-thread synchronization mechanism for a large number of concurrent read and write requests to achieve maximum concurrent execution, thereby improving the overall performance of the software system.

发明内容Contents of the invention

本发明针对实时数据库管理系统、关系数据库管理系统、中间件的线程间同步的实时性需求和性能要求,设计并提出了一种实时快速的多线程访问共享资源的同步方法。The invention designs and proposes a real-time and fast multi-thread synchronization method for accessing shared resources aiming at the real-time requirements and performance requirements of thread synchronization in real-time database management systems, relational database management systems and middleware.

本发明要解决的技术问题The technical problem to be solved in the present invention

在研发软件系统过程中通常会遇到多线程并发访问共享资源的问题,如果并发度比较低,用简单的互斥量(Mutex)就可以实现。但是,在并发度较高时,一些高性能软件系统也要求多线程间的同步操作具有较高的性能。并发的多线程按照是否修改共享资源可以分为两类:只读线程和修改线程,一个好的多线程同步技术应该允许多个只读线程可以同时访问共享资源,其次更新线程应该在等待当前只读线程完成操作后即可马上获取共享资源,不会出现后继的只读线程从修改线程抢占共享资源的情况。由于主流操作系统如Windows 2000、WindowsXP以及Windows 2003 Server没有内置读写锁,因此通常需要开发人员耗费较长的时间来设计和实现线程间的读写锁,而且最终实现的效果并不一定能够满足软件系统对线程间同步的高性能要求。In the process of developing software systems, the problem of concurrent access to shared resources by multiple threads is usually encountered. If the degree of concurrency is relatively low, it can be realized with a simple mutex (Mutex). However, when the degree of concurrency is high, some high-performance software systems also require high performance in synchronous operations among multiple threads. Concurrent multithreading can be divided into two categories according to whether to modify shared resources: read-only threads and modifying threads. A good multi-thread synchronization technology should allow multiple read-only threads to access shared resources at the same time. Secondly, the update thread should wait for the current only After the read thread completes the operation, the shared resource can be obtained immediately, and the subsequent read-only thread will not seize the shared resource from the modifying thread. Since mainstream operating systems such as Windows 2000, WindowsXP, and Windows 2003 Server do not have built-in read-write locks, it usually takes a long time for developers to design and implement read-write locks between threads, and the final effect may not be satisfactory. The high-performance requirements of software systems for synchronization between threads.

本发明针对这类技术问题,提出了一种通用的、实时快速的线程间的读写锁解决方案如下,给出了读写锁的具体实现细节。一方面,可以大大减少开发人员的时间,另一方面,可以满足软件系统对多个读写线程之间进行同步的性能要求。Aiming at such technical problems, the present invention proposes a universal, real-time and fast inter-thread read-write lock solution as follows, and provides specific implementation details of the read-write lock. On the one hand, it can greatly reduce the time of developers, on the other hand, it can meet the performance requirements of the software system for synchronization between multiple read and write threads.

一种读写锁实现方法,其步骤包括:A method for implementing a read-write lock, the steps comprising:

1)初始化读写锁,根据读写锁组件创建读临界区、写临界区和写通知事件三个对象,所述读临界区和所述写临界区初始化为可进入状态,所述写通知事件初始化为被触发状态;1) Initialize the read-write lock, create three objects according to the read-write lock component: read critical area, write critical area and write notification event, the read critical area and the write critical area are initialized to enterable state, and the write notification event Initialized to the triggered state;

2)当多个读线程和/或写线程并发访问共享资源,根据所述临界区保护所述共享资源内关键代码,设置读写锁;2) When multiple reading threads and/or writing threads access the shared resource concurrently, according to the critical section protecting the key code in the shared resource, a read-write lock is set;

2-1)申请读锁接口时,先进入写临界区再进入读临界区,根据读锁个数阻止写或读线程的操作,同时所述写通知事件设为非触发状态;释放读锁接口时,进入读临界区,若此时读锁个数为0则同时将所述写通知事件设为触发状态;2-1) When applying for the read lock interface, first enter the write critical area and then enter the read critical area, prevent the operation of the write or read thread according to the number of read locks, and set the write notification event to a non-triggered state; release the read lock interface , enter the read critical area, and if the number of read locks is 0 at this time, set the write notification event to the trigger state at the same time;

2-2)申请写锁接口时,先进入写临界区再进入读临界区,根据写锁个数阻止写或读线程的操作,同时所述写通知事件设为非触发状态;释放写锁接口时;进入读临界区,同时所述写通知事件设为触发状态;2-2) When applying for the write lock interface, first enter the write critical area and then enter the read critical area, prevent the operation of the write or read thread according to the number of write locks, and set the write notification event to a non-triggered state; release the write lock interface time; enter the read critical area, and simultaneously the write notification event is set to trigger state;

3)遍历所述2-1)至2-2)返回读写锁信息完成读写锁设置,实现资源共享。3) Go through the above 2-1) to 2-2) and return the read-write lock information to complete the read-write lock setting and realize resource sharing.

所述读写锁组件中定义:公开成员构造函数CReadWriteLock和析构函数~CReadWriteLock;申请读锁接口函数ReadLock和释放读锁接口函数ReadUnlock;申请写锁WriteLock接口函数和释放写锁接口函数WriteUnlock。The read-write lock component defines: public member constructor CReadWriteLock and destructor ~CReadWriteLock; application read lock interface function ReadLock and release read lock interface function ReadUnlock; application write lock WriteLock interface function and release write lock interface function WriteUnlock.

所述读写锁组件中还定义了四个私有成员:最大读锁个数m_ulMaxReadCount;读临界区m_hReadCriticalSection;写临界区m_hWriteCriticalSection;写通知事件m_hWriteNotifyEvent。The read-write lock component also defines four private members: the maximum number of read locks m_ulMaxReadCount; read critical section m_hReadCriticalSection; write critical section m_hWriteCriticalSection; write notification event m_hWriteNotifyEvent.

所述最大读锁个数m_ulMaxReadCount默认为5。The maximum number of read locks m_ulMaxReadCount is 5 by default.

所述步骤2-2)中申请写锁时,设置一单位为毫秒级的超时等待时间,在超过所述等待时间后仍未获得写锁即返回失败。When applying for a write lock in the step 2-2), set a timeout waiting time with a unit of milliseconds, and return failure if the write lock is not obtained after exceeding the waiting time.

更进一步,所述步骤2-1)中,根据读锁个数阻止写或读线程的操作方法如下:Further, in said step 2-1), the operation method of preventing writing or reading threads according to the number of read locks is as follows:

2-1-1)进入所述两个临界区后,若发现当前读锁个数已经达到最大数目,则返回失败,否则说明当前线程可继续进行读锁操作;2-1-1) After entering the two critical sections, if it is found that the current number of read locks has reached the maximum number, then return failure, otherwise the current thread can continue to read lock operations;

2-1-2)当所述步骤2-1-1)中当前读锁数目未达到最大数目,可增加当前读锁个数,如果增加后读锁个数大于0,则不能允许写线程进行操作。2-1-2) When the number of current read locks in the step 2-1-1) does not reach the maximum number, the current number of read locks can be increased. If the number of read locks after the increase is greater than 0, the write thread cannot be allowed to perform operate.

更进一步,所述步骤2-2)中,根据写锁个数阻止写或读线程的操作的方法如下:Further, in the step 2-2), the method of preventing the operation of writing or reading threads according to the number of write locks is as follows:

2-2-1)先进入写临界区阻止其它线程申请读锁或写锁,然后在设定的等待时间内等待写通知事件以确保没有其它线程对共享资源进行读写操作;2-2-1) First enter the write critical area to prevent other threads from applying for read locks or write locks, and then wait for the write notification event within the set waiting time to ensure that no other threads read or write shared resources;

2-2-2)若在所述设定的等待时间内写通知事件没有被触发,则等待超时,返回失败;若在指定时间内写通知事件被触发,则说明此时已没有读线程,获得独占的写锁,然后将写通知事件设置为非触发状态,只要有读锁或写锁存在,就不允许新的写锁申请,返回成功;2-2-2) If the write notification event is not triggered within the set waiting time, the wait times out and returns fail; if the write notification event is triggered within the specified time, it means that there is no reading thread at this time, Obtain an exclusive write lock, and then set the write notification event to a non-triggered state. As long as there is a read lock or write lock, no new write lock application is allowed, and the return is successful;

2-2-3)所述步骤2-2-2)如果返回成功,申请写锁线程不会离开写临界区,当返回失败,申请写锁的线程就要马上离开写临界区。2-2-3) Step 2-2-2) If the return is successful, the thread applying for the write lock will not leave the write critical area, and when the return fails, the thread applying for the write lock will immediately leave the write critical area.

更进一步,所述释放读锁过程中,只进入读临界区,不进入写临界区,然后将读锁个数减1,当减到0时,将通知事件设为触发状态,退出读临界区;Furthermore, in the process of releasing the read lock, only enter the read critical area, not the write critical area, and then reduce the number of read locks by 1, and when it is reduced to 0, set the notification event as a trigger state, and exit the read critical area ;

更进一步,所述释放写锁过程中,先将写通知事件设为触发状态,再退出写临界区。Furthermore, in the process of releasing the write lock, first set the write notification event as a trigger state, and then exit the write critical section.

更进一步,所述读写锁在Windows 2000、Windows XP以及Windows 2003 Server中没有内置读写锁的操作系统中进行操作。Furthermore, the read-write lock operates in Windows 2000, Windows XP and Windows 2003 Server without built-in read-write lock.

读写锁解决方案主要涉及三个部分,读写锁进行并行申请、并行处理:1)读写锁初始化过程;2)申请与释放读锁;3)申请与释放写锁。这三部分都包含在本发明实现的读写锁类CReadWriteLock中,作为类的公开成员函数给外部使用,内部采用两个临界区对象分别对读、写关键代码进行保护,使用事件对象来通知写锁的状态。由于临界区的速度远远快于互斥量,因此采用临界区量可以获得较高的运行效率。类CReadWriteLock内部使用一个变量记录当前的读锁个数,在申请、释放锁时都要判断与最大读锁个数的关系,从而决定申请锁的流程。The read-write lock solution mainly involves three parts. The read-write lock is applied in parallel and processed in parallel: 1) the initialization process of the read-write lock; 2) apply for and release the read lock; 3) apply for and release the write lock. These three parts are all included in the read-write lock class CReadWriteLock realized by the present invention, which is used as a public member function of the class for external use, and two critical area objects are used to protect the key codes of reading and writing respectively, and event objects are used to notify write The state of the lock. Since the speed of the critical section is much faster than that of the mutex, the use of the critical section can obtain higher operating efficiency. Class CReadWriteLock internally uses a variable to record the current number of read locks. When applying and releasing locks, it must judge the relationship with the maximum number of read locks to determine the process of applying for locks.

在读写锁初始化过程中,分别创建和初始化读临界区、写临界区和写通知事件三个对象,读、写临界区初始化为可进入状态,写通知事件初始化为被触发状态,从而可以接受写锁申请,然后初始化当前读锁个数为0,即也可以接收读锁申请。During the initialization process of the read-write lock, three objects of the read critical area, write critical area and write notification event are created and initialized respectively. The read and write critical areas are initialized to enterable state, and the write notification event is initialized to be triggered state, so that it can accept Write lock application, and then initialize the current number of read locks to 0, that is, read lock applications can also be received.

在申请读锁过程中,需要读取和修改当前读锁个数,因此使用临界区来保护这段代码,临界区分为两个,一个是写临界区,用来保护写锁相关的操作,一个是读临界区,用来保护读锁相关的操作。由于写锁是排它性的,因此必须先进入写临界区,然后进入读临界区,而退出时的顺序则相反,先退出读临界区,再退出写临界区,这样就能避免出现死锁的情况。进入两个临界区后,如果发现当前读锁个数已经达到最大数目,则返回失败,否则说明当前线程还可以得到读锁,于是增加当前读锁个数,如果增加后读锁个数大于0,则不能允许写线程进行操作,此时还可以继续增加读锁个数,只要不超过设置的最大读锁个数即可。有读锁时就不允许申请写锁。所以还要设置写通知事件为非触发状态,阻止其它线程获得写锁。In the process of applying for a read lock, it is necessary to read and modify the number of current read locks, so a critical section is used to protect this code. The critical section is divided into two, one is a write critical section, which is used to protect operations related to write locks, and the other is a critical section. It is a read critical section, which is used to protect operations related to read locks. Since the write lock is exclusive, you must first enter the write critical section, then enter the read critical section, and the order of exit is reversed, first exit the read critical section, and then exit the write critical section, so as to avoid deadlock Case. After entering the two critical sections, if it is found that the current number of read locks has reached the maximum number, it will return failure, otherwise it means that the current thread can still get read locks, so increase the current number of read locks, if the number of read locks is greater than 0 after the increase , the write thread cannot be allowed to operate. At this time, you can continue to increase the number of read locks, as long as it does not exceed the set maximum number of read locks. When there is a read lock, it is not allowed to apply for a write lock. Therefore, it is also necessary to set the write notification event to a non-triggered state to prevent other threads from obtaining a write lock.

在释放读锁过程中,由于此时读锁个数大于0,不可能有线程获得写锁,因此只需要进入读临界区,而无需进入写临界区,然后将读锁个数减1,当减到0时说明此时没有线程对共享资源进行读操作了,因此可以允许线程进行写操作,通过设置写通知事件为触发状态来唤醒正在等待的写线程。In the process of releasing the read lock, since the number of read locks is greater than 0 at this time, it is impossible for any thread to obtain the write lock, so it is only necessary to enter the read critical section instead of the write critical section, and then reduce the number of read locks by 1, when When it is reduced to 0, it means that there is no thread to read the shared resource at this time, so the thread can be allowed to write, and the waiting writing thread can be woken up by setting the write notification event as the trigger state.

在申请写锁过程中,先要进入写临界区来阻止其它线程申请读锁或写锁,即确保不会被后继申请读写锁的线程抢占,然后等待写通知事件以确保没有其它线程对共享资源进行读写操作,这个等待过程可以设置超时时间,以提高申请写锁的成功率,若在指定时间内写通知事件没有被触发,则等待超时,返回失败。如果在指定时间内写通知事件被触发,则说明此时已没有读线程,申请者可以获得独占的写锁,然后将写通知事件设置为非触发状态,返回成功只要有读锁或写锁存在,就不允许新的写锁申请。返回成功。如果返回成功,为了确保写锁的独占性,申请写锁线程不会离开写临界区,使得其它申请读写锁的线程在进入写临界区这一步就被阻塞;当返回失败时,申请写锁的线程就要马上离开写临界区。In the process of applying for a write lock, you must first enter the write critical area to prevent other threads from applying for read or write locks, that is, to ensure that they will not be preempted by subsequent threads that apply for read-write locks, and then wait for write notification events to ensure that no other threads are on the shared lock. The resource performs read and write operations. This waiting process can set a timeout period to improve the success rate of applying for a write lock. If the write notification event is not triggered within the specified time, the waiting timeout will return failure. If the write notification event is triggered within the specified time, it means that there is no read thread at this time, the applicant can obtain an exclusive write lock, and then set the write notification event to a non-triggered state, and the return is successful as long as there is a read lock or a write lock. , new write lock applications are not allowed. Return success. If the return is successful, in order to ensure the exclusiveness of the write lock, the thread applying for the write lock will not leave the write critical area, so that other threads applying for the read-write lock will be blocked at the step of entering the write critical area; when the return fails, apply for the write lock The thread will leave the write critical section immediately.

在释放写锁过程中,由于在申请读锁时就进入了写临界区但是没有退出来,因此在释放写锁时就不需要进入写临界区,只要先将写通知事件设为触发状态,然后退出写临界区即可。In the process of releasing the write lock, since the write critical area was entered but not exited when the read lock was applied, there is no need to enter the write critical area when the write lock is released, as long as the write notification event is set to the trigger state first, and then Just exit the write critical section.

本发明的有益效果Beneficial effects of the present invention

根据以上技术解决方案可以看出,本发明具有以下有益效果:According to the above technical solutions, it can be seen that the present invention has the following beneficial effects:

1、本发明提出了以临界区和事件组合实现读写锁的方法,具有简单易用、轻量快速的特点。本方法采用临界区作为主要互斥工具,避免了使用内核态对象时的用户状态频繁切换的开销,大大提高了读写锁的性能。1. The present invention proposes a method for realizing a read-write lock by combining a critical section and an event, which is easy to use, lightweight and fast. The method adopts the critical section as the main mutual exclusion tool, avoids the overhead of frequent switching of the user state when using the kernel state object, and greatly improves the performance of the read-write lock.

2、本发明为大量读线程和少量写线程的场景做了如下优化:读线程与读线程之间没有冲突,并且在已有读锁时再申请读锁的开销非常小;写线程不会无限等待,只需要等待到申请写锁时的读线程都完成操作,而不会被后面申请读锁的线程抢占共享资源。2. The present invention optimizes the scenario of a large number of read threads and a small number of write threads as follows: there is no conflict between the read threads and the read threads, and the overhead of applying for a read lock when there is already a read lock is very small; the write thread will not be infinite Waiting, you only need to wait until the reading threads that apply for the write lock complete the operation, and the shared resources will not be preempted by the thread that applies for the read lock later.

3、本发明提供了一个读写锁实现类,具有简洁的申请锁、释放锁的调用接口,可以很方便的集成到各种软件系统中。3. The present invention provides a read-write lock implementation class, which has a concise call interface for applying for and releasing locks, and can be easily integrated into various software systems.

附图说明Description of drawings

图1是本发明读写锁实现方法一实施例中申请读锁的流程图;Fig. 1 is a flow chart of applying for a read lock in an embodiment of the read-write lock implementation method of the present invention;

图2是本发明读写锁实现方法一实施例中释放读锁的流程图;Fig. 2 is the flowchart of releasing the read lock in an embodiment of the read-write lock implementation method of the present invention;

图3是本发明读写锁实现方法一实施例中申请写锁的流程图;Fig. 3 is a flow chart of applying for a write lock in an embodiment of the read-write lock implementation method of the present invention;

图4是本发明读写锁实现方法一实施例中释放写锁的流程图;Fig. 4 is a flow chart of releasing the write lock in an embodiment of the read-write lock implementation method of the present invention;

图5是本发明读写锁实现方法的流程示意图。Fig. 5 is a schematic flowchart of a method for implementing a read-write lock in the present invention.

具体实施方式Detailed ways

下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,可以理解的是,所描述的实施例仅仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. It should be understood that the described embodiments are only some of the embodiments of the present invention, not all of them. example. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative efforts belong to the protection scope of the present invention.

如图5所示是本发明读写锁实现方法的流程示意图,读写锁解决方案主要涉及三个部分,读写锁进行并行申请、并行处理:1)读写锁初始化过程;2)申请与释放读锁;3)申请与释放写锁。As shown in Figure 5, it is a flow diagram of the implementation method of the read-write lock of the present invention. The read-write lock solution mainly involves three parts, and the read-write lock performs parallel application and parallel processing: 1) the read-write lock initialization process; 2) application and Release the read lock; 3) apply and release the write lock.

读写锁组件Read-write lock component

读写锁类定义如下:The read-write lock class is defined as follows:

[1]class CReadWriteLock{[1] class CReadWriteLock{

[2]public:[2] public:

[3]CReadWriteLock(ULONG ulMaxReadCount=5);[3] CReadWriteLock(ULONG ulMaxReadCount=5);

[4]virtual~CReadWriteLock();[4] virtual~CReadWriteLock();

[5]virtual BOOL ReadLock();[5] virtual BOOL ReadLock();

[6]virtual void ReadUnLock();[6] virtual void ReadUnLock();

[7]virtual BOOL WriteLock(ULONG ulWaitMSTime=INFINITE);[7] virtual BOOL WriteLock(ULONG ulWaitMSTime=INFINITE);

[8]virtual void WriteUnLock();[8] virtual void WriteUnLock();

[9]protected:[9]protected:

[10]ULONG m_ulMaxReadCount;[10]ULONG m_ulMaxReadCount;

[11]CRITICAL_SECTION m_hReadCriticalSection;[11] CRITICAL_SECTION m_hReadCriticalSection;

[12]CRITICAL_SECTION m_hWriteCriticalSection;[12] CRITICAL_SECTION m_hWriteCriticalSection;

[13]HANDLE m_hWriteNotifyEvent;[13] HANDLE m_hWriteNotifyEvent;

[14]};[14]};

其中定义了六个公共方法:CReadWriteLock、~CReadWriteLock是构造函数和析构函数;ReadLock、ReadUnlock是申请读锁和释放读锁的接口函数;WriteLock、WriteUnlock是申请写锁和释放写锁的接口函数。Among them, six public methods are defined: CReadWriteLock and ~CReadWriteLock are constructors and destructors; ReadLock and ReadUnlock are interface functions for applying for and releasing read locks; WriteLock and WriteUnlock are interface functions for applying for and releasing write locks.

另外还定义了四个私有成员:m_ulMaxReadCount表示最大读锁个数,默认为5;m_hReadCriticalSection是对读操作进行保护的临界区,即读临界区,m_hWriteCriticalSection是对写操作进行保护的临界区,即写临界区;m_hWriteNotifyEvent是写通知事件,用于指示写锁的状态。In addition, four private members are defined: m_ulMaxReadCount indicates the maximum number of read locks, and the default is 5; m_hReadCriticalSection is the critical section for protecting read operations, that is, read critical section; m_hWriteCriticalSection is the critical section for protecting write operations, that is, write Critical section; m_hWriteNotifyEvent is a write notification event used to indicate the state of the write lock.

初始化接口方法实现Initialize interface method implementation

类CReadWriteLock的构造函数为初始化接口方法,定义为:The constructor of class CReadWriteLock is an initialization interface method, defined as:

CReadWriteLock::CReadWriteLock(ULONG ulMaxReadCount=5);CReadWriteLock::CReadWriteLock(ULONG ulMaxReadCount=5);

其中参数ulMaxReadCount用来设置读写锁允许的最大并发读锁个数,默认为5,。一般说来,最大并发读锁个数不宜设置太大,否则会影响性能。The parameter ulMaxReadCount is used to set the maximum number of concurrent read locks allowed by the read-write lock, and the default is 5. Generally speaking, the maximum number of concurrent read locks should not be set too large, otherwise performance will be affected.

初始化接口方法的具体实现代码如下:The specific implementation code of the initialization interface method is as follows:

[1]::InitializeCriticalSection(&m_hReadCriticalSection);[1]::InitializeCriticalSection(&m_hReadCriticalSection);

[2]::InitializeCriticalSection(&m_hWriteCriticalSection);[2]::InitializeCriticalSection(&m_hWriteCriticalSection);

[3]m_hWriteNotifyEvent=::CreateEvent(0,TRUE,TRUE,0);[3] m_hWriteNotifyEvent=::CreateEvent(0,TRUE,TRUE,0);

[4]m_ulReadCount=ulMaxReadCount;[4] m_ulReadCount = ulMaxReadCount;

该方法主要对读写锁用到的临界区、事件等对象调用系统接口进行初始化,写通知事件被初始化为触发状态,同时设置最大并发读锁个数。This method mainly initializes the critical area, event and other objects used by the read-write lock to call the system interface, the write notification event is initialized to the trigger state, and the maximum number of concurrent read locks is set at the same time.

如图1所示是本发明读写锁实现方法一实施例中申请读锁的流程图;先将返回值设置为TRUE,进入写临界区,进入两个临界区后,开始对读锁个数和写通知事件进行操作,接着判断读锁个数如果已到最大个数,则将返回值设置为FALSE,否则将读锁个数增加1。然后判断读锁个数是否为1,如果是则说明刚刚有线程申请读锁,因此应拒绝其它线程申请写锁,因此重置写通知事件为非触发状态;完成上述操作后,离开临界区;最后将设定的返回值返回给调用者。As shown in Figure 1 is the flow chart of applying for a read lock in an embodiment of the read-write lock implementation method of the present invention; the return value is set to TRUE earlier, enters the write critical area, and after entering two critical areas, starts to read the number of locks Operate with the write notification event, and then judge that if the number of read locks has reached the maximum number, set the return value to FALSE, otherwise, increase the number of read locks by 1. Then judge whether the number of read locks is 1. If it is, it means that a thread has just applied for a read lock, so other threads should be rejected to apply for a write lock, so reset the write notification event to a non-triggered state; after completing the above operations, leave the critical section; Finally, return the set return value to the caller.

以下是申请读锁接口方法实现中申请读锁接口方法具体定义:The following is the specific definition of the interface method for applying for a read lock in the implementation of the interface method for applying for a read lock:

BOOL CReadWriteLock::ReadLock();BOOL CReadWriteLock::ReadLock();

如果申请读锁成功,则返回TRUE;如果申请读锁失败,则返回FALSE。If the application for the read lock is successful, it returns TRUE; if the application for the read lock fails, it returns FALSE.

申请读锁接口方法具体实现代码如下:The specific implementation code of the interface method for applying for a read lock is as follows:

[1]BOOL bSucc=TRUE;[1] BOOL bSucc = TRUE;

[2]::EnterCriticalSection(&m_hWriteCriticalSection);[2]::EnterCriticalSection(&m_hWriteCriticalSection);

[3]::EnterCriticalSection(&m_hReadCriticalSection);[3]::EnterCriticalSection(&m_hReadCriticalSection);

[4]if(m_ulReadCount>=m_ulMaxReadCount)[4] if(m_ulReadCount>=m_ulMaxReadCount)

[5] bSucc=FALSE;[5] bSucc = FALSE;

[6]else{[6] else {

[7]m_ulReadCount++;[7] m_ulReadCount++;

[8]if(m_ulReadCount==1)[8] if(m_ulReadCount==1)

[9]::ResetEvent(m_hWriteNotifyEvent);[9]::ResetEvent(m_hWriteNotifyEvent);

[10]}[10]}

[11]::LeaveCriticalSection(&m_hReadCriticalSection);[11]::LeaveCriticalSection(&m_hReadCriticalSection);

[12]::LeaveCriticalSection(&m_hWriteCriticalSection);[12]::LeaveCriticalSection(&m_hWriteCriticalSection);

[13]return bSucc;[13] return bSucc;

其中,第1行先将返回值设置为TRUE;第2行进入写临界区,第3行进入读临界区;进入两个临界区后,开始对读锁个数和写通知事件进行操作,第4行判断读锁个数如果已到最大个数,则在第5行将返回值设置为FALSE,否则第7行将读锁个数增加1,然后第8行判断读锁个数是否为1,如果是则说明刚刚有线程申请读锁,因此应拒绝其它线程申请写锁,因此在第9行重置写通知事件为非触发状态;完成上述操作后,在第11、12行离开临界区;最后将设定的返回值返回给调用者。Among them, the first line first sets the return value to TRUE; the second line enters the write critical area, and the third line enters the read critical area; after entering the two critical areas, it starts to operate on the number of read locks and write notification events, and the fourth line If the number of read locks has reached the maximum number, set the return value to FALSE in line 5, otherwise, increase the number of read locks by 1 in line 7, and then determine whether the number of read locks is 1 in line 8, if yes It means that a thread has just applied for a read lock, so other threads should be rejected to apply for a write lock, so reset the write notification event to a non-triggered state on line 9; after completing the above operations, leave the critical section on lines 11 and 12; finally set The set return value is returned to the caller.

图2所示是本发明读写锁实现方法一实施例中释放读锁的流程图,申请进入读临界区;进入临界区后,将读锁个数减1;接着判断读锁个数,如果等于0则说明此时没有读锁了,设置写通知事件为触发状态,使得正在申请写锁的线程可以获得写锁;离开读临界区。Shown in Fig. 2 is the flow chart of releasing the read lock in one embodiment of the implementation method of the read-write lock of the present invention, and the application enters the read critical area; after entering the critical area, the number of read locks is subtracted by 1; then the number of read locks is judged, if If it is equal to 0, it means that there is no read lock at this time. Set the write notification event to the trigger state, so that the thread that is applying for the write lock can obtain the write lock; leave the read critical section.

以下是释放读锁接口方法实现中释放读锁接口方法具体定义:The following is the specific definition of the release read lock interface method in the implementation of the release read lock interface method:

void CReadWriteLock::ReadUnLock()void CReadWriteLock::ReadUnLock()

该方法内部相对较简单,没有定义返回值。The method is relatively simple internally, and no return value is defined.

释放读锁接口方法的具体实现代码如下:The specific implementation code of the release read lock interface method is as follows:

[1]::EnterCriticalSection(&m_hReadCriticalSection);[1]::EnterCriticalSection(&m_hReadCriticalSection);

[2]m_ulReadCount--;[2] m_ulReadCount--;

[3]if(m_ulReadCount==0)[3] if(m_ulReadCount==0)

[4]  ::SetEvent(m_hWriteNotifyEvent);[4] ::SetEvent(m_hWriteNotifyEvent);

[5]::LeaveCriticalSection(&m_hReadCriticalSection);[5]::LeaveCriticalSection(&m_hReadCriticalSection);

其中第1行申请进入读临界区;进入临界区后,第2行则将读锁个数减1;第3行判断读锁个数,如果等于0则说明此时没有读锁了,因此在第4行设置写通知事件为触发状态,使得正在申请写锁的线程可以获得写锁;第5行是离开读临界区。Among them, the first line applies to enter the read critical area; after entering the critical area, the second line will reduce the number of read locks by 1; the third line judges the number of read locks, if it is equal to 0, it means that there is no read lock at this time, so in Line 4 sets the write notification event to the triggered state, so that the thread that is applying for the write lock can obtain the write lock; line 5 is to leave the read critical section.

图3所示是本发明读写锁实现方法一实施例中申请写锁的流程图,其中,申请进入写临界区;进入写临界区后,等待写通知事件并设置了等待超时时间,如果在指定时间内写通知事件被触发则成功返回,此时并不退出临界区,这是因为写操作是独占的,不允许其它线程同时拥有写锁,因此将其它申请读写锁的线程阻止在进入写临界区这个初始步骤;如果在指定时间内写通知事件没有被触发,这意味着这段时间内读线程并没有完成所有操作,因此不能进行写操作,所以退出写临界区,以便于后继线程申请读写锁,然后返回FALSE。Figure 3 is a flow chart of applying for a write lock in an embodiment of the read-write lock implementation method of the present invention, wherein the application enters the write critical area; after entering the write critical area, wait for the write notification event and set the waiting timeout time, if If the write notification event is triggered within the specified time, it will return successfully. At this time, it will not exit the critical section. This is because the write operation is exclusive, and other threads are not allowed to have write locks at the same time, so other threads that apply for read-write locks are blocked from entering The initial step of writing the critical section; if the write notification event is not triggered within the specified time, it means that the reading thread has not completed all operations during this period, so the writing operation cannot be performed, so exit the writing critical section, so that the subsequent thread Apply for a read-write lock, then return FALSE.

以下是申请写锁接口方法实现申请写锁接口方法的具体定义:The following is the specific definition of the interface method of applying for a write lock to implement the interface method of applying for a write lock:

BOOL CReadWriteLock::WriteLock(ULONG ulWaitMSTime);BOOL CReadWriteLock::WriteLock(ULONG ulWaitMSTime);

其中参数ulWaitMSTime是申请写锁的等待时间,单位为毫秒,如果超过该时间仍然未能申请成功,表明此次申请操作超时失败,返回FALSE;如果在指定时间内申请成功,则返回TRUE。The parameter ulWaitMSTime is the waiting time for applying for a write lock, in milliseconds. If the application fails after this time, it indicates that the application operation has timed out and returns FALSE; if the application is successful within the specified time, it returns TRUE.

申请写锁接口方法的具体实现代码如下:The specific implementation code of the interface method for applying for a write lock is as follows:

[1]::EnterCriticalSection(&m_hWriteCriticalSection);[1]::EnterCriticalSection(&m_hWriteCriticalSection);

[2]if(WAIT_OBJECT_0==::WaitForSingleObject(m_hWriteNotifyEvent,ulWaitMSTime))[2] if(WAIT_OBJECT_0==::WaitForSingleObject(m_hWriteNotifyEvent,ulWaitMSTime))

[3]  return TRUE;[3] return TRUE;

[4]else{[4] else {

[5]  ::LeaveCriticalSection(&m_hWriteCriticalSection);[5] ::LeaveCriticalSection(&m_hWriteCriticalSection);

[6]  return FALSE;[6] return FALSE;

[7]}[7]}

其中,第1行申请进入写临界区;进入写临界区后,在第2行等待写通知事件并设置了等待超时时间,如果在指定时间内写通知事件被触发则在第3行成功返回,此时并不退出临界区,这是因为写操作是独占的,不允许其它线程同时拥有写锁,因此将其它申请读写锁的线程阻止在进入写临界区这个初始步骤;如果在指定时间内写通知事件没有被触发,这意味着这段时间内读线程并没有完成所有操作,因此不能进行写操作,所以在第5行退出写临界区,以便于后继线程申请读写锁,然后在第6行返回FALSE。Among them, line 1 applies to enter the write critical area; after entering the write critical area, wait for the write notification event in line 2 and set the waiting timeout period. If the write notification event is triggered within the specified time, it will return successfully in line 3. At this time, the critical section is not exited. This is because the write operation is exclusive and other threads are not allowed to own the write lock at the same time. Therefore, other threads applying for the read-write lock are prevented from entering the initial step of writing the critical section; if within the specified time The write notification event is not triggered, which means that the read thread has not completed all operations during this period, so the write operation cannot be performed, so exit the write critical section on line 5 so that subsequent threads can apply for read-write locks, and then Line 6 returns FALSE.

图4所示是本发明读写锁实现方法一实施例中释放写锁的流程图,重置写通知事件为触发状态,然后退出保护写操作的临界区,以便于后继线程申请读写锁。Figure 4 is a flow chart of releasing the write lock in an embodiment of the read-write lock implementation method of the present invention, reset the write notification event to the trigger state, and then exit the critical section protecting the write operation, so that subsequent threads apply for the read-write lock.

以下是释放写锁接口方法实现释放写锁接口方法的具体定义:The following is the specific definition of the release write lock interface method to implement the release write lock interface method:

void CReadWriteLock::WriteUnLock();void CReadWriteLock::WriteUnLock();

该方法内部相对较简单,没有定义返回值。The method is relatively simple internally, and no return value is defined.

释放写锁接口方法的具体代码如下:The specific code for releasing the write lock interface method is as follows:

[1]::SetEvent(m_hWriteNotifyEvent);[1]::SetEvent(m_hWriteNotifyEvent);

[2]::LeaveCriticalSection(&m_hWriteCriticalSection);[2]::LeaveCriticalSection(&m_hWriteCriticalSection);

其中第1行是重置写通知事件为触发状态,然后第2行退出保护写操作的临界区,以便于后继线程申请读写锁。The first line is to reset the write notification event to the triggered state, and then the second line exits the critical section that protects the write operation, so that subsequent threads can apply for 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 (27)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
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
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 郑州云海信息技术有限公司 Data 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
CN114003397A (en) * 2021-11-02 2022-02-01 武汉深之度科技有限公司 Instruction execution method based on thread read-write lock
CN114327642A (en) * 2021-12-31 2022-04-12 深圳市兆珑科技有限公司 Data read-write control method and electronic equipment
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
CN119201485A (en) * 2023-06-25 2024-12-27 华为技术有限公司 Multithreading concurrent access method and device
CN117667316B (en) * 2023-12-05 2024-06-18 上海浦东发展银行股份有限公司 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软件操作系统公司 Process scheduler employing adaptive partitioning of process threads
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软件操作系统公司 Process scheduler employing adaptive partitioning of process threads
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
US5727203A (en) Methods and apparatus for managing a database in a distributed object operating environment using persistent and transient cache
CN106462395B (en) Thread in multiline procedure processor framework waits
US7512950B1 (en) Barrier synchronization object for multi-threaded applications
Rostedt et al. Internals of the RT Patch
CN100410872C (en) Method and apparatus for enhanced runtime host support
KR100985318B1 (en) Methods and products for transparent support of operating system services
US8510738B2 (en) Preventing unnecessary context switching by employing an indicator associated with a lock on a resource
CN111857993B (en) Method for calling user mode function in kernel mode
US20130047011A1 (en) System and Method for Enabling Turbo Mode in a Processor
US7526673B2 (en) Parallel processing system by OS for single processors and parallel processing program
US20100250809A1 (en) Synchronization mechanisms based on counters
CN102906703A (en) Automatic suspend and resume in hardware transactional memory
CN101546275B (en) Method for acquiring multiprocessor hardware semaphore
CN103473135A (en) Processing method for spinlock LHP (Lock-Holder Preemption) phenomenon under virtual environment
US20190073243A1 (en) User-space spinlock efficiency using c-state and turbo boost
CN112559210B (en) Shared resource read-write mutual exclusion method based on RTX real-time system
US20090007124A1 (en) Method and mechanism for memory access synchronization
Wolf et al. RTOS support for parallel execution of hard real-time applications on the MERASA multi-core processor
US11301304B2 (en) Method and apparatus for managing kernel services in multi-core system
Akgul et al. A system-on-a-chip lock cache with task preemption support
Fukuoka et al. An efficient inter-node communication system with lightweight-thread scheduling
US12079657B2 (en) Handling ownership of process threads
CN117271144B (en) Thread processing method and electronic equipment
CN116860465A (en) Methods and related equipment for implementing synchronization locks

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