Definition
Threads are like lightweight process
- They execute concurrently like process
- multiple threads can run simultaneously on multiple CPUs/cores
- Unlike processes, threads cohabitate the same address space
- Threads within a process see the same heap and globals and can communicate with each other through variables and memory
- But, they can interfere with each other, need synchronization for shared resources
- Each thread has its own stack
Why We Want to Use Threads
- Code Responsiveness
- While doing an expensive computation, you don’t what your interface to freeze
- Processor Utilization
- If one thread is waiting on a deep-hierarchy memory access you can still use that processor time
- Failure Isolation
- If one portion of your code fails, it will only crash that one portion