Threading

1. Thread Safe Class

  • Make the class immutable if possible
  • Create a custom concurrent queue
  • Use dispatch_sync for all readers
  • Use dispatch_barrier_async for all writers

2. Thread Safe Singleton There are two thread safety cases to consider

  • during initialization of the singleton instance
  • during reads and writes to the instance (internal variables)

3. Atomic vs Nonatomic

  • Atomic will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread.
  • Declaring a property as atomic results in implicit locking/unlocking around each access of this property.
  • Acquiring a lock on a resource always comes with a performance cost
  • What "atomic" does not do is make any guarantees about thread safety

4. Explain Grand Central Dispatch (GCD)

  • GCD is a library that provides a low-level API to run tasks concurrently while managing threads behind the scenes.
  • Dispatch Queues, A dispatch queue is responsible for executing a task in the first-in, first-out order.
  • Serial Dispatch Queue A serial dispatch queue runs tasks one at a time.
  • Concurrent Dispatch Queue A concurrent dispatch queue runs as many tasks as it can without waiting for the started tasks to finish.
  • Main Dispatch Queue A globally available serial queue that executes tasks on the application’s main thread.

5. What is the difference between synchronous and asynchronous task?

  • When you invoke something synchronously, it means that the thread that initiated that operation will wait for the task to finish before continuing. Asynchronous means that it will not wait.

6. What is the difference between atomic and non-atomic synthesized properties?

First, properties are set to atomic by default.

Atomic properties are more likely to guarentee thread-safety because it will ensure that a value is fully set (by the setter method) or fully retrieved (by the getter method) when accessor methods are called simultaneously.

Non-atomic properties, howeer are not thread-safe. While they run faster, they may cause race conditions. In the event that accessor methods are called simultaneously and a race condition occurs, a setter value would first release the old value and a getter method would retrieve nil since no value has not been set yet.

7. NSOperation — NSOperationQueue — NSBlockOperation

NSOperation adds a little extra overhead compared to GCD, but we can add dependency among various operations and re-use, cancel or suspend them. NSOperationQueue, It allows a pool of threads to be created and used to execute NSOperations in parallel. Operation queues aren’t part of GCD. NSBlockOperation allows you to create an NSOperation from one or more closures. NSBlockOperations can have multiple blocks, that run concurrently.

8. Readers-Writers

Multiple threads reading at the same time while there should be only one thread writing. The solution to the problem is a readers-writers lock which allows concurrent read-only access and an exclusive write access. Terminology; Race Condition A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Deadlock A deadlock occurs when two or sometimes more tasks wait for the other to finish, and neither ever does. Readers-Writers problem Multiple threads reading at the same time while there should be only one thread writing. Readers-writer lock Such a lock allows concurrent read-only access to the shared resource while write operations require exclusive access. Dispatch Barrier Block Dispatch barrier blocks create a serial-style bottleneck when working with concurrent queues.

9. Why do we use synchronized ? synchronized guarantees that only one thread can be executing that code in the block at any given time.

results matching ""

    No results matching ""