/ concurrency

Tasks and threads in Swift

I was talking about the threading system with somebody not so long time ago and I wanted to send him a tutorial, but I could not find one that covers the BASIC terms. So here it is, I'm going to expain the whole thing in this post from the very beginning.

CPU

We are living in the multicore-processor era. This means that inside your CPU there are multiple cores that can execute multiple tasks in parallel.

Task

A task is a unit of execution. A task can be a mathematical operation, some kind of I/O operation, or the combination of these. Each CPU core executes a single task at a time.

Multitasking

Multitasking allows the processor to switch between tasks that are being executed without having to wait for each task to finish.

Here is a really simple example for multitasking: if one task needs to do some I/O operation - like reading a huge file from disk - it can be "suspended" and the CPU can focus on a different task that actually needs real CPU time - like calculating some really difficult mathematical function.

Process

A process is a running instance of an application. Processes are the (binary) executable packages. Usually in your program code you define tasks to execute. Each process contains the current activity state of his own.

Thread

Until this point the definitions above are really obvious. The CPU can run tasks, it can multitask too, and the process is roughly what describes the tasks to execute. What the heck is a thread?

a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler

according to wikipedia

However, there is a better explanation on StackOverflow:

"A thread is an independent set of values for the processor registers (for a single core). Since this includes the Instruction Pointer (aka Program Counter), it controls what executes in what order. It also includes the Stack Pointer, which had better point to a unique area of memory for each thread or else they will interfere with each other."

If you still don't get it I'll translate this to human. So basically a thread is just a sub-group of tasks. Threads can be created by the processes, or the operating system. Threads can have priorities and the CPU can priorize tasks based on the thread priority. Ok, this is way too dumb, but that's ok for now.

Multithreading

Processes can have multiple threads of executions. Multithreading is the ability of the CPU to execute multiple threads (tasks on the threads or tasks made by the processes) concurrently. This is usually supported by the operating system. If you want to read more about threading you should check this article.

Event loop

The event loop is basically a loop that waits for tasks to execute. There is a special one, called main event loop, which controls your programs main flow. If you are familiar with node.js / javascript you probably already know what's an event loop. Just remember, the event loop waits for events to happen.

Stack

The stack is used to keep track of variables/parameters local to a function in a program. Whenever you call a new function, a new stack frame is pushed to the stack with parameters and variables local to that function. When that function returns, the stack frame is popped out and the context switches back to the previous function (the caller).

Heap

The heap is a global memory pool. A function can allocate memory on the heap if it wants the data to live longer than the function itself. Objects allocated on the heap are accessible to all the functions, given they have the reference/address of the object to access it.

Queue

A queue is an abstract data type, that stores multiple tasks & runs them in a "FIFO" order. A queue can have some other properties like priority, and the tasks can be executed on various threads. It's basically a higher level of abstraction around threads, to utilize tasks, but this is also a really dumb wording, you should check this stackoverflow question if you want to know what's the exact difference between a thread and a queue.


As you can see, it's really hard make an exact definition out of these terms, but I really hope that you are getting closer to understand the main logic behind the concurrency model in the world of computer science. In the second part of this series, I'm going to explain how Apple's - Swift - Foundation framework is helping you executing tasks in parallel and what are the main classes to support concurrent programming.