What is a thread?
- A thread is an independent thread of execution within a single process.
- OSes and/or programming languages allow processes to split themselves into two or more concurrently executing functions.
- Allows for intra-process concurrency (and even true parallelism on multiprocessor and/or multicore machines)
- Stack segment is subdivided into multiple miniature stacks.
- Thread manager time slices and switches between simultaneously running threads in much the same way that the kernel scheduler switches between processes. (In fact, threads are often called lightweight processes #srsly).
- Primary difference: threads have independent stacks, but they all share access to the same text, data, and heap segments.
- Pro: easier to support communication between threads, because address spaces accessible are largely the same.
- Pro: Multiple threads can access the same global data and one copy of the code.
- Pro: One thread can share its stack space (via pointers and references) with another thread.
- Con: No protected memory, since address space is shared. Memory errors are more common, and debugging is more difficult.
- Con: Multiple threads can access the same global data and one copy of the code. #doubleedgedsword
- Con: One thread can share its stack space (via pointers and references) with another thread. #mixedblessings