锁
iOS多线程锁有两类 自旋锁 和 互斥锁
自旋锁与互斥锁比较类似,它们都是为了解决对某项资源的互斥使用。资源已经被占用,资源申请者只能进入睡眠状态。但是自旋锁不会引起调用者睡眠,如果自旋锁已经被别的执行单元保持,调用者就一直循环在那里看是否该自旋锁的保持者已经释放了锁,"自旋"一词就是因此而得名。
具体有哪些锁
1. OSSpinLock
OSSpinLock叫做”自旋锁”,等待锁的线程会处于忙等(busy-wait)状态,一直占用着CPU资源
目前已经不再安全,可能会出现优先级反转问题,iOS10开始弃用。
如果等待锁的线程优先级较高,它会一直占用着CPU资源,优先级低的线程就无法释放锁
需要导入头文件#import
var moneyLock: OSSpinLock = OS_SPINLOCK_INIT
var ticketLock: OSSpinLock = OS_SPINLOCK_INIT
override func drawMoney() {
OSSpinLockLock(&moneyLock)
super.drawMoney()
OSSpinLockUnlock(&moneyLock)
}
override func saveMoney() {
OSSpinLockLock(&moneyLock)
super.saveMoney()
OSSpinLockUnlock(&moneyLock)
}
override func saleOneTicket() {
OSSpinLockLock(&ticketLock)
super.saleOneTicket()
OSSpinLockUnlock(&ticketLock)
}
2. os_unfair_lock
os_unfair_lock用于取代不安全的OSSpinLock ,从iOS10开始才支持
从底层调用看,等待os_unfair_lock锁的线程会处于休眠状态,并非忙等
需要导入头文件#import
var moneyLock: os_unfair_lock = os_unfair_lock()
var ticketLock: os_unfair_lock = os_unfair_lock()
override func drawMoney() {
os_unfair_lock_lock(&moneyLock)
super.drawMoney()
os_unfair_lock_unlock(&moneyLock)
}
override func saveMoney() {
os_unfair_lock_lock(&moneyLock)
super.saveMoney()
os_unfair_lock_unlock(&moneyLock)
}
override func saleOneTicket() {
os_unfair_lock_lock(&ticketLock)
super.saleOneTicket()
os_unfair_lock_unlock(&ticketLock)
}
3. pthread_mutex
mutex叫做”互斥锁”,等待锁的线程会处于休眠状态
需要导入头文件#import
- 普通锁
var ticketLockMutex: pthread_mutex_t = pthread_mutex_t()
var moneyLockMutex: pthread_mutex_t = pthread_mutex_t()
override init() {
pthread_mutex_init(&ticketLockMutex, nil)
pthread_mutex_init(&moneyLockMutex, nil)
}
override func drawMoney() {
pthread_mutex_lock(&moneyLockMutex)
super.drawMoney()
pthread_mutex_unlock(&moneyLockMutex)
}
override func saveMoney() {
pthread_mutex_lock(&moneyLockMutex)
super.saveMoney()
pthread_mutex_unlock(&moneyLockMutex)
}
override func saleOneTicket() {
pthread_mutex_lock(&ticketLockMutex)
super.saleOneTicket()
pthread_mutex_unlock(&ticketLockMutex)
}
deinit {
pthread_mutex_destroy(&ticketLockMutex)
pthread_mutex_destroy(&moneyLockMutex)
}
- 递归锁
var ticketLockMutex: pthread_mutex_t = pthread_mutex_t()
var count = 0
override init() {
super.init()
initMutex(&ticketLockMutex)
}
private func initMutex(_ mutex: inout pthread_mutex_t) {
var attr: pthread_mutexattr_t = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
pthread_mutex_init(&mutex, &attr)
pthread_mutexattr_destroy(&attr)
}
func otherTest() {
pthread_mutex_lock(&ticketLockMutex);
if count < 10 {
count += 1
otherTest()
}
print(count)
pthread_mutex_unlock(&ticketLockMutex)
}
deinit {
pthread_mutex_destroy(&ticketLockMutex)
}
- 条件锁
var data: [String] = []
var pThreadMutex: pthread_mutex_t = pthread_mutex_t()
var pThreadCond: pthread_cond_t = pthread_cond_t()
override init() {
super.init()
initMutex(&pThreadMutex)
}
private func initMutex(_ mutex: inout pthread_mutex_t) {
var attr: pthread_mutexattr_t = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
pthread_mutex_init(&mutex, &attr)
pthread_mutexattr_destroy(&attr)
pthread_cond_init(&pThreadCond, nil)
}
func otherTest() {
Thread(target: self, selector: #selector(remove), object: nil).start()
Thread(target: self, selector: #selector(add), object: nil).start()
}
@objc private func remove() {
pthread_mutex_lock(&pThreadMutex);
print("remove begin")
if data.count == 0 {
pthread_cond_wait(&pThreadCond, &pThreadMutex)
}
data.removeLast()
print("remove end")
pthread_mutex_unlock(&pThreadMutex)
}
@objc private func add() {
pthread_mutex_lock(&pThreadMutex);
sleep(1)
print("add begin")
data.append("Test")
print("add end")
pthread_cond_signal(&pThreadCond)
pthread_mutex_unlock(&pThreadMutex)
}
deinit {
pthread_mutex_destroy(&pThreadMutex)
pthread_cond_destroy(&pThreadCond)
}
4. NSLock是对mutex普通锁的封装
var ticketLock: NSLock = NSLock()
var moneyLock: NSLock = NSLock()
override func drawMoney() {
moneyLock.lock()
super.drawMoney()
moneyLock.unlock()
}
override func saveMoney() {
moneyLock.lock()
super.saveMoney()
moneyLock.unlock()
}
override func saleOneTicket() {
ticketLock.lock()
super.saleOneTicket()
ticketLock.unlock()
}
5. NSRecursiveLock也是对mutex递归锁的封装,API跟NSLock基本一致
var ticketLockMutex = NSRecursiveLock()
var count = 0
override init() {
super.init()
}
func otherTest() {
ticketLockMutex.lock()
if count < 10 {
count += 1
otherTest()
}
print(count)
ticketLockMutex.unlock()
}
串行队列
直接使用GCD的串行队列,也是可以实现线程同步的
var ticketQueue = DispatchQueue(label: "ticketQueue")
var moneyQueue = DispatchQueue(label: "moneyQueue")
override func drawMoney() {
ticketQueue.async {
super.drawMoney()
}
}
override func saveMoney() {
ticketQueue.async {
super.saveMoney()
}
}
override func saleOneTicket() {
ticketQueue.async {
super.saleOneTicket()
}
}
semaphore: 信号量
信号量的初始值,可以用来控制线程并发访问的最大数量
信号量的初始值为1,代表同时只允许1条线程访问资源,保证线程同步
var semaphore = DispatchSemaphore(value: 5)
var ticketSemaphore = DispatchSemaphore(value: 1)
var moneySemaphore = DispatchSemaphore(value: 1)
override func drawMoney() {
moneySemaphore.wait()
super.drawMoney()
moneySemaphore.signal()
}
override func saveMoney() {
moneySemaphore.wait()
super.saveMoney()
moneySemaphore.signal()
}
override func saleOneTicket() {
ticketSemaphore.wait()
super.saleOneTicket()
ticketSemaphore.signal()
}
func otherTest() {
for _ in 0..<20 {
Thread(target: self, selector: #selector(test), object: nil).start()
}
}
@objc private func test() {
// 如果信号量的值 > 0,就让信号量的值减1,然后继续往下执行代码
// 如果信号量的值 <= 0,就会休眠等待,直到信号量的值变成>0,就让信号量的值减1,然后继续往下执行代码
_ = semaphore.wait(wallTimeout: DispatchWallTime.distantFuture)
sleep(2)
print(Thread.current)
semaphore.signal()
}
- @synchronized是对mutex递归锁的封装
源码查看:objc4中的objc-sync.mm文件
@synchronized(obj)内部会生成obj对应的递归锁,然后进行加锁、解锁操作
// Objective-C
@synchronized(obj){
//TODO:
}
// Swift等价实现
func synchronized(_ lock: AnyObject, closure: () -> Void) {
objc_sync_enter(lock)
defer { objc_sync_exit(lock) }
closure()
}
拓展
atomic
用于保证属性setter、getter的原子性操作,相当于在getter和setter内部加了线程同步的锁
可以参考源码objc4的objc-accessors.mm
它并不能保证使用属性的过程是线程安全的, 多个线程仍然可以同时读写访问。
平常属性都不建议使用,getter和setter调用比较频繁,大量使用的话很消耗性能