P2L2 Threads and Concurrency Flashcards
Give a metaphor for a thread: A thread is like ….
List 3 attributes:
- ….
- ….
- …..
.. a worker in a toy shop
- An active entity
- Works simultaneously
- Needs coordination
Explain the difference between a thread and a process
A process has both address space and execution context information.
Threads represent multiple independent execution contexts within the same address space
address space (virtual <-> physical memory mapping)
- code
- data
- heap
execution context
- CPU registers
- stack
State 2 benefits of multithreading
- Threads can run in parallel since they are executing different instructions of the same code.
- Specialization: For instance, we can give higher priority to tasks that handle more important tasks or service higher paying customers.
What’s the advantage of multi-thread over multi-process? Give 2 reasons.
- Each process needs its own address space.
- -> more memory usage.
- -> more memory swaps. - Inter process communication (IPC) - is more costly than inter thread communication, which consists primarily of reading/writing shared variables.
Are threads useful on a single CPU? Explain why or why not.
Yes - to hide latency.
For example if a thread makes a disk request I/O - if it takes 2x as long as context switch then it’s a great deal. The thread is stuck anyway on a slow I/O request, so it pays to context switch to another thread who can continue working.