Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of the terms "queues", "multicore", and "threads" in Grand Central Dispatch

Tags:

I am trying to get my head around the concepts of Grand Central Dispatch. I want to understand these quotes from Vandad's book on Concurrent Programming.

The real use for GCD is to dispatch tasks to multiple cores, without making you the programmer, worry about which core is executing which task.

and

At the heart of GCD are dispatch queues. Dispatch queues are pools of threads.

and finally

You will not be working with these threads directly. You will just work with dispatch queues, dispatching tasks to these queues and asking queues to invoke your task.

I have bolded the key terms.

Are multiple cores the same as queues? Does a queue consist of many threads? Does each thread perform a task?

like image 900
Cescy Avatar asked Jan 18 '14 10:01

Cescy


People also ask

Which of the following queues are used in Grand Central Dispatch?

Understanding Queues As mentioned before, GCD operates on dispatch queues through a class aptly named DispatchQueue . You submit units of work to this queue, and GCD executes them in a FIFO order (first in, first out), guaranteeing that the first task submitted is the first one started.

What are threads and queues?

Threads → Queues — If you use worker threads to synchronize the execution of tasks, use a serial queue. — If you use worker threads to execute tasks with no interdependencies, use a concurrent queue. For thread pools, encapsulate the task in a function or an object and dispatch them to a concurrent queue for execution.

What is Dispatch queue?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

How gcd works in iOS?

Overview. Dispatch, also known as Grand Central Dispatch (GCD), contains language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in macOS, iOS, watchOS, and tvOS.


2 Answers

So multiple cores are the same as queues?

Not really. A queue is a programming abstraction, a core is a physical resource in your processor. There is no unique relationship between a queue and a core, although at any given point in time it can be said that a given queue is executing a given task on a given core.

A queue consists of many threads?

A queue consists of tasks. Tasks are assigned to threads by the queue managing system when it comes the time to execute that task. Threads are OS resources and are allocated to cores, which effectively run them and have no notion of what a task is (except for Hyper-Threading CPUs).

If you do not account for hardware-multithreading (e.g., Hyper-threading), at any given point in time a core is running a specific thread; when it comes the time to run a different thread, a context-switch occurs in that core. If you account for hardware-multithreading, you can have multiple threads running on virtual cores hosted in the same physical core.

The relationship between queues and threads is opaque. A queue could manage several threads at once, or several threads once at a time, or just one all the time; in the first case, you have a parallel queue, able to execute parallel tasks on simultaneous threads; in the second and third case, you have a serial queue.

Each thread performs a task?

At any given point in time, a thread is performing a task. You can have threads that are spawn, execute their task, and die; or you can have long running threads (i.e., the main thread) that execute several tasks.

Maybe it is pretty puzzling at start, you might need some reading about Operating Systems and maybe high-level Processor Architectures to fully understand this.

GCD aims at letting you reason exclusively in abstract terms: i.e., in terms of tasks and queues, and forget about threads and cores, that are seen as a sort of "implementation means", or low-level details that you can leave to the system to use efficiently.

like image 65
sergio Avatar answered Sep 30 '22 00:09

sergio


Queues are just list of tasks to execute, cores depend on the processor, you can have 1 or many cores. Queues are configurable and you can decide if tasks can be executed concurently or not, if you allow concurency in your queue, tasks in the queue can be executed at the same time in different cores.

like image 44
bsarr007 Avatar answered Sep 30 '22 00:09

bsarr007