Skip to content

Instantly share code, notes, and snippets.

@yxztj
Last active March 22, 2019 08:38
Show Gist options
  • Save yxztj/321eac8b40496637b6f224c7a7d1fa8c to your computer and use it in GitHub Desktop.
Save yxztj/321eac8b40496637b6f224c7a7d1fa8c to your computer and use it in GitHub Desktop.
concurrent execution with nested write lock would cause deadlock
import UIKit
private var imp = pthread_rwlock_t()
func wlock(_ time: Int) {
pthread_rwlock_wrlock(&imp)
print("[\(time)]wlock")
}
func rlock(_ time: Int) {
pthread_rwlock_rdlock(&imp)
print("[\(time)]rlock")
}
func unlock(_ time: Int) {
pthread_rwlock_unlock(&imp)
print("[\(time)]unlock")
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pthread_rwlock_init(&imp, nil)
DispatchQueue.concurrentPerform(iterations: 1000) { (time) in
wlock(time)
print("[\(time)]w1")
// concurrent execution with nested write lock would cause deadlock
// the issue won't repro with read lock.
wlock(time)
print("[\(time)]w2")
unlock(time)
unlock(time)
}
}
}
@Arthraim
Copy link

:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment