绾跨▼瀹夊叏: 浜掓枼閿佸拰鑷棆閿?10绉?
Posted iOS寮€鍙?/a> 绾跨▼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绾跨▼瀹夊叏: 浜掓枼閿佸拰鑷棆閿?10绉?相关的知识,希望对你有一定的参考价值。
浣滆€呬辅Dariel
https://www.jianshu.com/p/a321377c5639
鏃犲苟鍙?涓嶇紪绋?鎻愬埌澶氱嚎绋嬪氨寰堥毦缁曞紑閿侌煍?
ios寮€鍙戜腑杈冨父瑙佺殑涓ょ被閿?
1. 浜掓枼閿? 鍚屼竴鏃跺埢鍙兘鏈変竴涓嚎绋嬭幏寰椾簰鏂ラ攣,鍏朵綑绾跨▼澶勪簬鎸傝捣鐘舵€?
2. 鑷棆閿? 褰撴煇涓嚎绋嬭幏寰楄嚜鏃嬮攣鍚?鍒殑绾跨▼浼氫竴鐩村仛寰幆,灏濊瘯鍔犻攣,褰撹秴杩囦簡闄愬畾鐨勬鏁颁粛鐒舵病鏈夋垚鍔熻幏寰楅攣鏃?绾跨▼涔熶細琚寕璧?
鑷棆閿佽緝閫傜敤浜庨攣鐨勬寔鏈夎€呬繚瀛樻椂闂磋緝鐭殑鎯呭喌涓?瀹為檯浣跨敤涓簰鏂ラ攣浼氱敤鐨勫涓€浜?
1. 浜掓枼閿?淇″彿閲?/span>
1.閬靛畧NSLocking鍗忚鐨勫洓绉嶉攣
鍥涚閿佸垎鍒槸:
NSLock銆丯SConditionLock銆丯SRecursiveLock銆丯SCondition
NSLocking鍗忚
public protocol NSLocking {
public func lock()
public func unlock()
}
涓嬮潰涓句釜澶氫釜鍞エ鐐瑰悓鏃跺崠绁ㄧ殑渚嬪瓙
var ticket = 20
var lock = NSLock()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let thread1 = Thread(target: self, selector: #selector(saleTickets), object: nil)
thread1.name = "鍞エ鐐笰"
thread1.start()
let thread2 = Thread(target: self, selector: #selector(saleTickets), object: nil)
thread2.name = "鍞エ鐐笲"
thread2.start()
}
@objc private func saleTickets() {
while true {
lock.lock()
Thread.sleep(forTimeInterval: 0.5) // 妯℃嫙寤惰繜
if ticket > 0 {
ticket = ticket - 1
print("\(String(describing: Thread.current.name!)) 鍗栧嚭浜嗕竴寮犵エ,褰撳墠杩樺墿\(ticket)寮犵エ")
lock.unlock()
}else {
print("oh 绁ㄥ凡缁忓崠瀹屼簡")
lock.unlock()
break;
}
}
}
閬靛畧鍗忚鍚庡疄鐜扮殑涓や釜鏂规硶lock()鍜寀nlock(),鎰忓鍏跺悕.
闄ゆ涔嬪NSLock銆丯SConditionLock銆丯SRecursiveLock銆丯SCondition鍥涚浜掓枼閿佸悇鏈夊叾瀹炵幇:
1. 闄SCondition澶?涓夌閿侀兘鏈夌殑涓や釜鏂规硶:
// 灏濊瘯鍘婚攣,濡傛灉鎴愬姛,杩斿洖true,鍚﹀垯杩斿洖false
open func `try`() -> Bool
// 鍦╨imit鏃堕棿涔嬪墠鑾峰緱閿?娌℃湁杩斿洖NO
open func lock(before limit: Date) -> Bool
2. NSCondition鏉′欢閿?
// 褰撳墠绾跨▼鎸傝捣
open func wait()
// 褰撳墠绾跨▼鎸傝捣,璁剧疆涓€涓敜閱掓椂闂?/span>
open func wait(until limit: Date) -> Bool
// 鍞ら啋鍦ㄧ瓑寰呯殑绾跨▼
open func signal()
// 鍞ら啋鎵€鏈塏SCondition鎸傝捣鐨勭嚎绋?/span>
open func broadcast()
褰撹皟鐢╳ait()涔嬪悗,NSCondition瀹炰緥浼氳В閿佸凡鏈夐攣鐨勫綋鍓嶇嚎绋?鐒跺悗鍐嶄娇绾跨▼浼戠湢,褰撹signal()閫氱煡鍚?绾跨▼琚敜閱?鐒跺悗鍐嶇粰褰撳墠绾跨▼鍔犻攣,鎵€浠ョ湅璧锋潵濂藉儚wait()涓€鐩存寔鏈夎閿?浣嗘牴鎹嫻鏋滄枃妗d腑璇存槑,鐩存帴鎶妛ait()褰撶嚎绋嬮攣骞朵笉鑳戒繚璇佺嚎绋嬪畨鍏?
3. NSConditionLock鏉′欢閿?
NSConditionLock鏄€熷姪NSCondition鏉ュ疄鐜扮殑,鍦∟SCondition鐨勫熀纭€涓婂姞浜嗛檺瀹氭潯浠?鍙嚜瀹氫箟绋嬪害鐩稿NSCondition浼氶珮浜?
// 閿佺殑鏃跺€欒繕闇€瑕佹弧瓒砪ondition
open func lock(whenCondition condition: Int)
// 鍚宼ry,鍚屾牱闇€瑕佹弧瓒砪ondition
open func tryLock(whenCondition condition: Int) -> Bool
// 鍚?span class="mq-175">unlock,闇€瑕佹弧瓒砪ondition
open func unlock(withCondition condition: Int)
// 鍚?span class="mq-179">lock,闇€瑕佹弧瓒砪ondition鍜屽湪limit鏃堕棿涔嬪墠
open func lock(whenCondition condition: Int, before limit: Date) -> Bool
4. NSRecurisiveLock閫掑綊閿?
瀹氫箟浜嗗彲浠ュ娆$粰鐩稿悓绾跨▼涓婇攣骞朵笉浼氶€犳垚姝婚攣鐨勯攣.
鎻愪緵鐨勫嚑涓柟娉曞拰NSLock绫讳技.
2. GCD鐨凞ispatchSemaphore鍜屾爡鏍忓嚱鏁?/span>
1. DispatchSemaphore淇″彿閲?
DispatchSemaphore涓殑淇″彿閲?鍙互瑙e喅璧勬簮鎶㈠崰鐨勯棶棰?鏀寔淇″彿鐨勯€氱煡鍜岀瓑寰?姣忓綋鍙戦€佷竴涓俊鍙烽€氱煡,鍒欎俊鍙烽噺+1;姣忓綋鍙戦€佷竴涓瓑寰呬俊鍙锋椂淇″彿閲?1,濡傛灉淇″彿閲忎负0鍒欎俊鍙蜂細澶勪簬绛夊緟鐘舵€?鐩村埌淇″彿閲忓ぇ浜?寮€濮嬫墽琛?鎵€浠ユ垜浠竴鑸皢DispatchSemaphore鐨剉alue璁剧疆涓?.
涓嬮潰缁欏嚭浜咲ispatchSemaphore鐨勫皝瑁呯被
class GCDSemaphore {
// MARK: 鍙橀噺
fileprivate var dispatchSemaphore: DispatchSemaphore!
// MARK: 鍒濆鍖?/span>
public init() {
dispatchSemaphore = DispatchSemaphore(value: 0)
}
public init(withValue: Int) {
dispatchSemaphore = DispatchSemaphore(value: withValue)
}
// 鎵ц
public func signal() -> Bool {
return dispatchSemaphore.signal() != 0
}
public func wait() {
_ = dispatchSemaphore.wait(timeout: DispatchTime.distantFuture)
}
public func wait(timeoutNanoseconds: DispatchTimeInterval) -> Bool {
if dispatchSemaphore.wait(timeout: DispatchTime.now() + timeoutNanoseconds) == DispatchTimeoutResult.success {
return true
} else {
return false
}
}
}
2. barrier鏍呮爮鍑芥暟:
鏍呮爮鍑芥暟涔熷彲浠ュ仛绾跨▼鍚屾,褰撶劧浜嗚繖涓偗瀹氭槸瑕佸苟琛岄槦鍒椾腑鎵嶈兘璧蜂綔鐢?鍙湁褰撳綋鍓嶇殑骞惰闃熷垪鎵ц瀹屾瘯,鎵嶄細鎵ц鏍呮爮闃熷垪.
/// 鍒涘缓骞跺彂闃熷垪
let queue = DispatchQueue(label: "queuename", attributes: .concurrent)
/// 寮傛鍑芥暟
queue.async {
for _ in 1...5 {
print(Thread.current)
}
}
queue.async {
for _ in 1...5 {
print(Thread.current)
}
}
/// 鏍呮爮鍑芥暟
queue.async(flags: .barrier) {
print("barrier")
}
queue.async {
for _ in 1...5 {
print(Thread.current)
}
}
3. 鍏朵粬鐨勪簰鏂ラ攣
1. pthread_mutex浜掓枼閿?/span>
pthread琛ㄧずPOSIX thread,璺ㄥ钩鍙扮殑绾跨▼鐩稿叧鐨凙PI,pthread_mutex涔熸槸涓€绉嶄簰鏂ラ攣,浜掓枼閿佺殑瀹炵幇鍘熺悊涓庝俊鍙烽噺闈炲父鐩镐技,闃诲绾跨▼骞剁潯鐪?闇€瑕佽繘琛屼笂涓嬫枃鍒囨崲.
涓€鑸儏鍐典笅,涓€涓嚎绋嬪彧鑳界敵璇蜂竴娆¢攣,涔熷彧鑳藉湪鑾峰緱閿佺殑鎯呭喌涓嬫墠鑳介噴鏀鹃攣,澶氭鐢宠閿佹垨閲婃斁鏈幏寰楃殑閿侀兘浼氬鑷村穿婧?鍋囪鍦ㄥ凡缁忚幏寰楅攣鐨勬儏鍐典笅鍐嶆鐢宠閿?绾跨▼浼氬洜涓虹瓑寰呴攣鐨勯噴鏀捐€岃繘鍏ョ潯鐪犵姸鎬?鍥犳灏变笉鍙兘鍐嶉噴鏀鹃攣锛屼粠鑰屽鑷存閿?
杩欒竟缁欏嚭浜嗕竴涓熀浜巔thread_mutex_t(瀹夊叏鐨?FIFO"浜掓枼閿?鐨勫熀鏈皝瑁?MutexLock
1. @synchronized鏉′欢閿?/span>
鏃ュ父寮€鍙戜腑鏈€甯哥敤鐨勫簲璇ユ槸@synchronized,杩欎釜鍏抽敭瀛楀彲浠ョ敤鏉ヤ慨楗颁竴涓彉閲?骞朵负鍏惰嚜鍔ㄥ姞涓婂拰瑙i櫎浜掓枼閿?杩欐牱,鍙互淇濊瘉鍙橀噺鍦ㄤ綔鐢ㄨ寖鍥村唴涓嶄細琚叾浠栫嚎绋嬫敼鍙?浣嗘槸鍦╯wift涓畠宸茬粡涓嶅瓨鍦ㄤ簡.鍏跺疄@synchronized鍦ㄥ箷鍚庡仛鐨勪簨鎯呮槸璋冪敤浜唎bjc_sync涓殑objc_sync_enter鍜宱bjc_sync_exit 鏂规硶锛屽苟涓斿姞鍏ヤ簡涓€浜涘紓甯稿垽鏂?
鍥犳鎴戜滑鍙互鍒╃敤闂寘鑷繁灏佽涓€濂?
func synchronized(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
// 浣跨敤
synchronized(lock: AnyObject) {
// 姝ゅAnyObject涓嶄細琚叾浠栫嚎绋嬫敼鍙?/span>
}
2. 鑷棆閿?/span>
1. OSSpinLock鑷棆閿?/span>
鎵ц鏁堢巼鏈€楂樼殑閿?涓嶈繃鍦╥OS10.0浠ュ悗搴熷純浜嗚繖绉嶉攣鏈哄埗,浣跨敤os_unfair_lock鏇挎崲,
椤惧悕鎬濅箟鑳藉淇濊瘉涓嶅悓浼樺厛绾х殑绾跨▼鐢宠閿佺殑鏃跺€欎笉浼氬彂鐢熶紭鍏堢骇鍙嶈浆闂.
璇﹁澶хibireme鐨?/span>涓嶅啀瀹夊叏鐨?OSSpinLock
https://blog.ibireme.com/2016/01/16/spinlock_is_unsafe_in_ios/
2. os_unfair_lock鑷棆閿?/span>
瀵逛簬os_unfair_lock,闈濬IFO鐨勯攣,鑻规灉涓轰簡鍙栦唬OSSPinLock鏂板嚭鐨勪竴涓攣,涓€涓珮绾х殑鑳藉閬垮厤浼樺厛绾у甫鏉ョ殑姝婚攣闂鐨勪竴涓攣,OSSPinLock涔嬪墠灏辩垎鍑哄湪iOS骞冲彴鏈夌敱浜庝紭鍏堢骇閫犳垚姝婚攣鐨勯棶棰?.
娉ㄦ剰: 杩欎釜閿侀€傜敤浜庡皬鍦烘櫙涓嬬殑涓€涓珮鏁堥攣,鍚﹀垯浼氬ぇ閲忔秷鑰梒pu璧勬簮.
var unsafeMutex = os_unfair_lock()
os_unfair_lock_lock(&unsafeMutex)
os_unfair_lock_trylock(&unsafeMutex)
os_unfair_lock_unlock(&unsafeMutex)
杩欒竟缁欏嚭浜嗗熀浜巓s_unfair_lock鐨勫皝瑁?MutexLock
https://github.com/DarielChen/iOSTips/blob/master/SwiftTipsDemo/DCTool/DCTool/MutexLock.swift
3. 鎬ц兘姣旇緝
杩欒竟璐翠竴寮犲ぇ绁瀒bireme鍦╥Phone6銆乮OS9瀵瑰悇绉嶉攣鐨勬€ц兘娴嬭瘯鍥?/span>
鎺ㄨ崘鈫撯啌鈫?nbsp;
闀?/strong>
鎸?/strong>
鍏?/strong>
娉?/strong>
馃憠銆?/strong>銆戦兘鍦ㄨ繖閲岋紒
娑电洊锛氱▼搴忓憳澶у挅銆佹簮鐮佸叡璇汇€佺▼搴忓憳鍏辫銆佹暟鎹粨鏋勪笌绠楁硶銆侀粦瀹㈡妧鏈拰缃戠粶瀹夊叏銆佸ぇ鏁版嵁绉戞妧銆佺紪绋嬪墠绔€丣ava銆丳ython銆乄eb缂栫▼寮€鍙戙€丄ndroid銆乮OS寮€鍙戙€丩inux銆佹暟鎹簱鐮斿彂銆佸菇榛樼▼搴忓憳绛夈€?/span>
以上是关于绾跨▼瀹夊叏: 浜掓枼閿佸拰鑷棆閿?10绉?的主要内容,如果未能解决你的问题,请参考以下文章
GitHub 鍏ㄩ儴婧愪唬鐮佽娉勯湶锛孴ypeScript 鐨勫紑鍙戣€呰川鐤戞墭绠℃湇鍔$殑瀹夊叏鎬э紒
鎻愰珮瀹夊叏鎬ц€屽湪HTTP鍝嶅簲澶翠腑鍙互浣跨敤鐨勫悇绉嶅搷搴斿ご瀛楁
淇濊瘉Linux绯荤粺瀹夊叏涔嬮厤缃甪irewalld闃茬伀澧欑殑鍦板潃浼鍙婄鍙h浆鍙戝疄渚嬶紝鍙窡鍋氾紒锛侊紒