Theory of Multithreading

Basic

Concurrency vs Parallelism

Concurrency - when things run at the same time (doesnt have to be related tasks)

Parallelism - when multiple parts of a program run at the same time (might be related tasks)

Pros of Concurrency

Thread Life-Cycle

How many threads?

threads ≤ cores on machine

Synchronization

Synchronisation is needed since without it, threads may clash when reading and writing data, resulting in false results.

Deadlock - when X thread is waiting for Y thread, but Y thread is waiting on X thread

Mutex

A thread locks a block of code,

no other thread can then run that code until it is unlocked