Contributing

What is NSLock?

What is NSLock?

An NSLock is a mutex; it prevents multiple threads from accessing the same resource simultaneously, which is exactly what you want to do here. Once one thread acquires the lock, other threads attempting to acquire the lock will wait until the first thread releases the lock.

What is NSRecursiveLock?

NSRecursiveLock defines a lock that may be acquired multiple times by the same thread without causing a deadlock, a situation where a thread is permanently blocked waiting for itself to relinquish a lock.

What is Os_unfair_lock?

The os_unfair_lock mutex (mutual exclusion) lock is currently the fastest lock in iOS. If your intention is to simply prevent two threads from accessing some piece of code at the same time (called a critical section) like in our EventQueue example, then this lock will get the job done with great performance.

Are swift properties Atomic?

Swift properties are non atomic by default. But — According to Apple’s Using Swift with Cocoa and Objective-C (Swift 3.1):

What is Dispatchsemaphore?

An object that controls access to a resource across multiple execution contexts through use of a traditional counting semaphore.

Is Swift array thread safe?

The Swift collection types like Array and Dictionary are not thread-safe when declared mutable. Although many threads can read a mutable instance of Array simultaneously without issue, it’s not safe to let one thread modify the array while another is reading it.

What is DispatchGroup in iOS?

DispatchGroup allows for aggregate synchronization of work. You can use them to submit multiple different work items and track when they all complete, even though they might run on different queues. This behavior can be helpful when progress can’t be made until all of the specified tasks are complete.

What is KVC and KVO in Swift?

KVC stands for Key-Value Coding. It’s a mechanism by which an object’s properties can be accessed using string’s at runtime rather than having to statically know the property names at development time. KVO stands for Key-Value Observing and allows a controller or class to observe changes to a property value.

What is DispatchWorkItem?

Overview. A DispatchWorkItem encapsulates work to be performed on a dispatch queue or within a dispatch group. You can also use a work item as a DispatchSource event, registration, or cancellation handler.

Is array copy thread-safe?

Arrays are not, in general, thread safe.

How are NSLock objects used in an application?

An object that coordinates the operation of multiple threads of execution within the same application. An NSLock object can be used to mediate access to an application’s global data or to protect a critical section of code, allowing it to run atomically. The NSLock class uses POSIX threads to implement its locking behavior.

How does the NSLock class use POSIX Threads?

The NSLock class uses POSIX threads to implement its locking behavior. When sending an unlock message to an NSLock object, you must be sure that message is sent from the same thread that sent the initial lock message. Unlocking a lock from a different thread can result in undefined behavior.

What to do when NSLock lock is not locked?

Use the NSRecursiveLock class to implement recursive locks instead. Unlocking a lock that is not locked is considered a programmer error and should be fixed in your code. The NSLock class reports such errors by printing an error message to the console when they occur.

Can a recursive lock be used on a different thread?

Unlocking a lock from a different thread can result in undefined behavior. You should not use this class to implement a recursive lock. Calling the lock method twice on the same thread will lock up your thread permanently. Use the NSRecursiveLock class to implement recursive locks instead.