Theory of Multithreading
Basic
- Threads share cache and translation lookaside buffer
- There must be only one main thread path
- Concurrency - the idea of processing several tasks at the same time
- Process-based multitasking - concurrent processes
- thread-based multitasking - concurrent threads
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
- Speed
- better on multi core
- process wont get blocked while waiting for say and IO
- Availability and Distribution
- most real world things are happening simultaneously
- Controllability
- functions can be started, stopped or interrupted by other concurrent functions
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