Threads - Chapter 4
Thread - light weight process created by a process

Each thread has its own:
- Program Counter
- Stack
- Registers
They share:
- Code
- Data section
- Files
- Heap
Advantages
Responsiveness - applications appear to be running at the same time
Resource sharing - threads can be working on the same data
Economy - easier to make a thread than a process
Scalable - threads can run on multi-cores
Concurrent vs Parallelism

Concurrent - multiple threads/processes are running multi tasking (can be one or multiple cores)
Parallel - multiple threads/processes can be running at the same time (must be multiple cores)
Challenges associated with programming threads
Knowing what should be parallel
Threads should have an equal work load
Data must be split to run on separate cores
Data access must be synchronised
Hard to debug, and test
Types of parallelism
Data parallelism - data is divided into chunks that can be processes by separate cores performing the same task
Task parallelism - each core is performing a separate operation
User-threads and Kernel-threads
User-thread:
- threads that work on the user-level
- can only be referenced within the process
- used by the programmer
- all user threads run on the same core
Kernel-thread:
- threads supported by the kernel
- resides within a process
- can be referenced by other processes
- not touched by the programmer
- one thread per core
- user threads can be assigned to a kernel thread
Threading models
Since each user thread needs to be assigned to a kernel thread, there are different ways of going about it
Many-to-one
- Many user to one kernel
- One block means all threads have to wait
- Does not run in parallel
One-to-one
- One kernel per one user
- Runs in parallel
- Can result in too many kernel threads, and then get thrown into virtual memory (lets be real here tho not gonna happen)
Many-to-many
- kernel decided the best fit, so that there are not too many kernel threads