Sobes.tech
Junior — Middle

Can you name the main primitives used in GCD (Grand Central Dispatch)?

sobes.tech AI

Answer from AI

The main primitives in Grand Central Dispatch (GCD) are:

  • Dispatch Queues: allow executing tasks asynchronously or synchronously. There are serial and concurrent queues.
  • Dispatch Groups: allow grouping multiple tasks and receiving a notification when all are completed.
  • Dispatch Semaphores: used to control access to resources, limiting the number of tasks executing simultaneously.
  • Dispatch Work Items: objects that encapsulate blocks of code, which can be run, canceled, or waited upon for completion.
  • Dispatch Sources: allow reacting to system events, such as timers, file changes, etc.

Example of creating and using a serial queue:

let serialQueue = DispatchQueue(label: "com.example.serialQueue")
serialQueue.async {
    print("Executing task in serial queue")
}