GCD Flashcards

1
Q

What is Grand Central Dispatch?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does Grand Central Dispatch work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between Grand Central Dispatch and Async Await?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why use Grand Central Dispatch over Async Await?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a race condition

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is data race?

A

A data race occurs when one thread accesses a mutable object while another thread is writing to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can Grand Central Dispatch prevent race condition?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a deadlock

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can Grand Central Dispatch prevent deadlock?

A

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:

  1. 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.
  2. 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.
  3. Deadlock Detection: GCD incorporates deadlock detection mechanisms to identify and handle situations where tasks are waiting indefinitely for each other to complete.
  4. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a thread?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain Asynchronous and Synchronous?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

With Grand Central Dispatch, explain the serial and concurrent queues.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the benefits of using GCD for concurrency management?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does GCD handle thread creation and management behind the scenes?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the concept of dispatch groups in GCD and their use cases.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the potential pitfalls or challenges developers might face when using GCD?

A

Some common challenges include deadlocks, race conditions, excessive thread creation, and difficulty in debugging asynchronous code. It’s important for developers to understand GCD’s concepts thoroughly to avoid these issues.

17
Q

How does GCD support quality of service (QoS) levels for prioritizing tasks?

A

GCD provides different QoS levels (such as user-initiated, utility, background, etc.) to prioritize tasks based on their importance and impact on user experience. Developers can specify the desired QoS level when dispatching tasks to ensure optimal performance and responsiveness of the application.

18
Q

What is a Dispatch Queue?

A

A Dispatch Queue is a fundamental concept in Grand Central Dispatch (GCD) that manages the execution of tasks concurrently. It abstracts the underlying threading mechanism, allowing developers to dispatch tasks to be executed asynchronously or synchronously. Dispatch Queues can be either serial, where tasks are executed in the order they are added, or concurrent, allowing tasks to run concurrently.

19
Q

How does GCD handle task prioritization?

A

GCD provides Quality of Service (QoS) levels to prioritize tasks based on their importance and impact on system performance. Tasks dispatched to a higher QoS queue are given precedence over those dispatched to lower QoS queues. The system dynamically adjusts the scheduling of tasks based on the QoS level assigned to each dispatch queue.

20
Q

What are the benefits of using GCD for multithreading?

A

GCD abstracts away the complexities of managing threads, making it easier for developers to write concurrent code without directly dealing with low-level threading APIs. It optimizes the use of system resources by efficiently managing threads, leading to improved performance and responsiveness of applications. Additionally, GCD simplifies synchronization and coordination of concurrent tasks, reducing the likelihood of common concurrency issues such as deadlocks and race conditions.

21
Q

What is the role of Dispatch Groups in GCD?

A

Dispatch Groups in GCD provide a mechanism for monitoring the completion of a group of tasks dispatched asynchronously to different queues. Developers can use dispatch groups to coordinate the execution of multiple tasks and perform additional actions once all tasks in the group have completed. Dispatch Groups help manage the complexity of asynchronous code by allowing developers to wait for the completion of multiple tasks before proceeding.

22
Q

How does GCD interact with Swift’s memory management system?

A

GCD integrates seamlessly with Swift’s memory management system by employing Automatic Reference Counting (ARC). When closures or blocks are dispatched to a queue, GCD automatically manages memory for captured variables within the closure/block. This means that developers don’t need to explicitly manage memory for objects captured by closures when using GCD, as ARC takes care of deallocating memory when it’s no longer needed.