GCD Flashcards
What is Grand Central Dispatch?
Grand Central Dispatch (GCD) is a technology by Apple for managing concurrent operations. It allows developers to perform tasks concurrently without having to manage threads directly.
How does Grand Central Dispatch work?
GCD works by abstracting away the management of threads and providing developers with APIs to dispatch tasks asynchronously or synchronously across global or custom queues. It efficiently utilizes system resources and optimizes performance.
GCD is built on top of threads. Under the hood, it manages a shared thread pool. With GCD, you add blocks of code or work items to dispatch queues and GCD decides which thread to execute them on.
What is the difference between Grand Central Dispatch and Async Await?
Grand Central Dispatch is a lower-level API provided by Apple for managing concurrency in macOS, iOS, etc., while Async Await is a higher-level language feature found in some programming languages (e.g., C#, JavaScript) that simplifies asynchronous programming by allowing developers to write asynchronous code in a more synchronous style.
Why use Grand Central Dispatch over Async Await?
Grand Central Dispatch offers more fine-grained control over concurrency and resource management compared to Async Await. It’s suitable for performance-critical applications and provides efficient utilization of system resources
What is a race condition
A race condition is a situation in concurrent programming where the outcome of a program depends on the order of execution of concurrent operations. It can lead to unpredictable behavior and bugs in the program.
What is data race?
A data race occurs when one thread accesses a mutable object while another thread is writing to it.
How can Grand Central Dispatch prevent race condition?
Grand Central Dispatch prevents race conditions by serializing access to shared resources through the use of queues. By dispatching tasks to queues, GCD ensures that only one task accesses a shared resource at a time, thereby avoiding race conditions.
What is a deadlock
A deadlock is a situation in concurrent programming where two or more threads are waiting indefinitely for each other to release resources that they need to proceed. As a result, none of the threads can make progress.
How can Grand Central Dispatch prevent deadlock?
Grand Central Dispatch prevents deadlocks by enforcing a strict hierarchy of resource acquisition. It ensures that tasks dispatched to queues wait for resources in a way that prevents circular dependencies, thus avoiding deadlocks.
Grand Central Dispatch stops things from getting stuck by making sure tasks wait for resources in a certain order. This way, they don’t end up waiting for each other forever, which could cause a deadlock.
Grand Central Dispatch (GCD) can prevent deadlock through several mechanisms:
- Serial Queues: GCD provides serial queues where tasks are executed one at a time in the order they are added. This prevents concurrent access to resources, reducing the likelihood of deadlock.
- Concurrent Queues: GCD also offers concurrent queues, which allow multiple tasks to run simultaneously. However, GCD manages the execution of these tasks to prevent deadlocks by intelligently scheduling tasks based on available resources.
- Deadlock Detection: GCD incorporates deadlock detection mechanisms to identify and handle situations where tasks are waiting indefinitely for each other to complete.
- Async Dispatch: By default, GCD dispatches tasks asynchronously, meaning tasks are added to the queue and execution continues without waiting for the task to complete. This helps avoid situations where tasks are blocked waiting for each other to finish.
Overall, GCD’s design and management of queues and tasks help mitigate the risk of deadlock in concurrent programming scenarios.
What is a thread?
A thread is the smallest unit of execution within a process. Multiple threads can run concurrently within the same process, allowing for parallel execution of tasks.
Explain Asynchronous and Synchronous?
Synchronous execution means that tasks are performed one after the other in a sequential manner, while asynchronous execution means that tasks can be performed simultaneously or independently of each other, without waiting for each other to complete.
With Grand Central Dispatch, explain the serial and concurrent queues.
Serial queues in Grand Central Dispatch execute tasks in the order they are added, ensuring that only one task executes at a time. Concurrent queues, on the other hand, can execute multiple tasks simultaneously, allowing for parallel execution of tasks.
What are the benefits of using GCD for concurrency management?
GCD provides several benefits, including improved performance through efficient thread management, simplified code structure, enhanced responsiveness of applications, and automatic optimization for system resources.
How does GCD handle thread creation and management behind the scenes?
GCD abstracts away the complexities of thread creation and management by utilizing a pool of threads managed by the system. It dynamically adjusts the number of threads based on system workload and resource availability.
Explain the concept of dispatch groups in GCD and their use cases.
Dispatch groups in GCD allow developers to monitor the completion of a group of tasks dispatched asynchronously to different queues. They are useful for coordinating multiple asynchronous tasks and performing additional actions once all tasks in the group have completed.