reference

Ke.qq.com/course/3145…

File read and write security scheme

In essence, multiple read and single write operations are allowed. Only one read and write operation is allowed per unit time.

Read write

Only one thread can write at a time.

Multiple threads are allowed to read at the same time.

Both write and read operations are not allowed at the same time.

Read/write lock -> pthread_rwlock

Read/write locks are very simple to use, are mutex locks, and the threads waiting for the lock go to sleep.

The import#import <pthread.h>Declare @property (assign, nonatomic) pthread_rwLOCK_t lock; Pthread_rwlock_init (&_lock, NULL); Lock for read operations pthread_rwLOCK_rdlock (&_lock); Lock for write operations pthread_rwlock_wrlock(&_lock); Pthread_rwlock_unlock (&_lock); Pthread_rwlock_destroy (&_lock);Copy the code
Asynchronous fence calls -> dispatch_barrier_async

The concurrent queue passed in by this function must be created with dispatch_ queue_ CREATE.

Declare @property (strong, nonatomic) dispatch_queue_t queue; Queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT); Dispatch_async (self.queue, ^{[selfread]; }); Dispatch_barrier_async (self.queue, ^{[self write]; });Copy the code

Thread synchronization safety scheme

OSSpinLock

Spin-locks, which are no longer secure, may have priority inversion problems.

If the thread waiting for the lock has a higher priority, it will continue to occupy CPU resources, and the lower priority thread will not be able to release the lock.

The import#import <libkern/OSAtomic.h>Declare @property (assign, nonatomic) OSSpinLock lock; Self. lock = OS_SPINLOCK_INIT; Lock OSSpinLockLock (& _lock); Unlock OSSpinLockUnlock (& _lock);Copy the code
os_unfair_lock

Os_unfair_lock replaces insecure OSSpinLock with support starting with iOS10.

The import#import <os/lock.h>Declare @property (assign, nonatomic) OS_UNFAIR_lock moneyLock; Initialize lock self.moneyLock = OS_UNFAIR_LOCK_INIT; Lock os_unfair_lock_lock (& _moneyLock); Unlock os_unfair_lock_unlock (& _moneyLock);Copy the code
pthread_mutex

Mutex used by multiple platforms. Can be a normal lock, a recursive lock, or a conditional lock.

dispatch_semaphore

A semaphore is called a semaphore.

The initial value of a semaphore that can be used to control the maximum number of concurrent accesses by a thread.

The initial value of the semaphore is 1, which means that only one thread is allowed to access the resource at the same time to ensure thread synchronization.

dispatch_queue(DISPATCH_QUEUE_SERIAL)

The essence of thread synchronization is not to allow multiple threads to access the same resource at the same time, as long as the sequential access to resources on the line, so the direct use of GCD serial queue, thread synchronization can also be achieved.

NSLock

NSLock is an object-oriented wrapper around the pthread_mutex common mutex lock.

NSRecursiveLock

NSRecursiveLock is an object-oriented wrapper around the pthread_mutex recursive mutex.

NSCondition

NSCondition is an object – oriented encapsulation of the pthread_mutex conditional mutex without condition values.

NSConditionLock

NSConditionLock is a further encapsulation of NSCondition, and you can set specific condition values to make the threads execute in the specified order.

@synchronized

@synchronized is the encapsulation of a mutex recursive lock.

@synchronized(obj) internally generates a recursive lock corresponding to OBj, and then locks and unlocks the lock.

The implementation is the simplest, but the performance is the worst. Therefore, it is not recommended.

Synchronization schemes are ranked from highest to lowest in performance

os_unfair_lock

OSSpinLock

dispatch_semaphore

pthread_mutex

dispatch_queue(DISPATCH_QUEUE_SERIAL)

NSLock

NSCondition

pthread_mutex(recursive)

NSRecursiveLock

NSConditionLock

@synchronized

When to use spinlocks?

1> Threads are expected to wait a short time for locks

2> Locking code is often called, but multi-thread contention is rare

3> The CPU source is not tight

4> Multi-core processor

When is a mutex used?

1> The thread is expected to wait a long time for the lock

2> Single-core processor

3> Lock code has IO operation

4> Lock code complex code or large amount of loop

5> Lock code is very competitive

The difference between a spin lock and a mutex

The spin lock is busy waiting while it is waiting, consuming CPU.

While waiting, the mutex sleeps and consumes no CPU.

supplement

1. Ios spin-locks were disabled from 10 and are now mutexes.

#import <pthread.h> to use pthread_mutex

3. Os_unfair_lock, dispatch_semaphore, and pthread_mutex locks are recommended.

4. If this article violates privacy or other things, please contact me, I will rectify or delete in the first time.