Skip to content

Instantly share code, notes, and snippets.

@Lessica
Created April 17, 2021 13:42
Show Gist options
  • Save Lessica/caab4494866a1286baf1f79a6fbc020d to your computer and use it in GitHub Desktop.
Save Lessica/caab4494866a1286baf1f79a6fbc020d to your computer and use it in GitHub Desktop.
MutexLock: pthread_mutex_t wrapper in Swift
import Foundation
final class MutexLock {
private var mutex: pthread_mutex_t = {
var mutex = pthread_mutex_t()
pthread_mutex_init(&mutex, nil)
return mutex
}()
func tryLock() -> Bool {
return pthread_mutex_trylock(&mutex) == 0
}
func lock() {
pthread_mutex_lock(&mutex)
}
func unlock() {
pthread_mutex_unlock(&mutex)
}
deinit {
pthread_mutex_destroy(&mutex)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment