Created
April 17, 2021 13:42
-
-
Save Lessica/caab4494866a1286baf1f79a6fbc020d to your computer and use it in GitHub Desktop.
MutexLock: pthread_mutex_t wrapper in Swift
This file contains hidden or 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 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