Is DispatchSemaphore a good replacement for NSLock?

I used NSLockto synchronize the touch parts of the code, but I had problems due to the fact that they had to be unlocked from the same thread from which they were blocked. Then I discovered that the GCD DispatchSemaphoreseems to do the same, with the added convenience that they can be signaled from any stream. I was wondering, however, if this convenience comes at the cost of thread safety. Is it possible to replace

let lock = NSLock()
lock.lock()
// do things...
lock.unlock()

with

let semaphore = DispatchSemaphore(value: 1)
semaphore.wait()
// do things...
semaphore.signal()

or will i be facing thread safety issues anyway?

+4
source share
1 answer

, , -.

, . .

{semaphore.wait()
// do things...
semaphore.signal()}

, , , .

: https://priteshrnandgaonkar.imtqy.com/concurrency-with-swift-3/

+1

Source: https://habr.com/ru/post/1675031/


All Articles