Created
August 10, 2021 13:33
-
-
Save JarvisTheAvenger/c56144e1cf5d2615879cd6044d3fc21d to your computer and use it in GitHub Desktop.
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 Foundation | |
// NSRecursiveLock - It is used to write thread safe code | |
// Locks needs to be balanced. | |
class Logger { | |
let recursiveLock = NSRecursiveLock() | |
func doSomething() { | |
recursiveLock.lock() | |
createSomething() | |
recursiveLock.unlock() | |
} | |
func createSomething() { | |
recursiveLock.lock() | |
print("thread safe code!!!") | |
recursiveLock.unlock() | |
} | |
} | |
let logger = Logger() | |
logger.doSomething() | |
logger.createSomething() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment