Read/write lock is a synchronization mechanism for concurrent control of computer programs. It is also known as “shared-mutex lock” or multi-reader-singleton lock. Read operations can be concurrently reentrant, and write operations are mutually exclusive.

Realize the principle of

Two mutexes

Implemented using two mutex and an integer counter. The condition counter tracks blocked reader threads. The mutex rlock protects condition for use by readers. The mutex wlock ensures that write operations are mutually exclusive.

#import "JKRWLock.h" #import <pthread/pthread.h> pthread_mutex_t rlock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t wlock = PTHREAD_MUTEX_INITIALIZER; static int condition = 0; @implementation JKRWLock - (void)rLock {pthread_mutex_lock(& rLock); condition++; if (condition == 1) { pthread_mutex_lock(&wlock); } pthread_mutex_unlock(&rlock); } // Reader unlock - (void)rUnlock {pthread_mutex_lock(&rlock); condition--; if (condition == 0) { pthread_mutex_unlock(&wlock); } pthread_mutex_unlock(&rlock); } // writer lock - (void)wLock {pthread_mutex_lock(& wLock); } // writer unlock - (void)wUnlock {pthread_mutex_unlock(&wlock); } @endCopy the code

Condition variable + mutex

Read/write locks can be implemented using the condition variable cond and the ordinary mutex rwLOCK, the integer counter readCount (indicating the number of reads being made), and the Boolean flag WRITE (indicating that a write is being made).

#import "JKRWLock.h" #import <pthread/pthread.h> @interface JKRWLock () @property (nonatomic, assign) int readCount; @property (nonatomic, assign, getter=isWriting) BOOL write; @end // Read/write lock pthread_mutex_t rwlock = PTHREAD_MUTEX_INITIALIZER; Pthread_cond_t cond = PTHREAD_COND_INITIALIZER; @implementation JKRWLock - (void)rLock {pthread_mutex_lock(&rwlock); while (self.isWriting) { pthread_cond_wait(&cond, &rwlock); } self.readCount++; pthread_mutex_unlock(&rwlock); } // Reader unlock - (void)rUnlock {pthread_mutex_lock(&rwlock); self.readCount--; If (self.readCount == 0) {// Call a write thread pthread_cond_signal(&cond); } pthread_mutex_unlock(&rwlock); } // writer lock - (void)wLock {pthread_mutex_lock(&rwlock); while (self.isWriting || self.readCount > 0) { pthread_cond_wait(&cond, &rwlock); } self.write = YES; pthread_mutex_unlock(&rwlock); } // writer unlock - (void)wUnlock {pthread_mutex_lock(&rwlock); self.write = NO; // Call up multiple reading threads pthread_cond_broadcast(&cond); pthread_mutex_unlock(&rwlock); } @endCopy the code

Synchronous task + fence

Read the data

- (id)objectForKey:(NSString *)key { __block id obj; Dispatch_sync (self.concurrent_queue, ^{obj = [self.datacenterdic objectForKey:key]; }); return obj; }Copy the code

Write the data

- (void)setObject:(id)obj forKey:(NSString *)key {
    // Async fence call set data:
    dispatch_barrier_async(self.concurrent_queue, ^{
        [self.dataCenterDic setObject:obj forKey:key];
    });
}
Copy the code

conclusion

That’s how the read-write lock works and how it can be used. If you have a better way to achieve, welcome to private message me.