Last active
March 22, 2019 08:38
-
-
Save yxztj/321eac8b40496637b6f224c7a7d1fa8c to your computer and use it in GitHub Desktop.
concurrent execution with nested write lock would cause deadlock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:(