Lesson 9: POSIX Threads — Create and Join
A thread is a lightweight unit of execution inside a process. In this lesson we learn to create threads with pthread_create, wait for them with pthread_join, and understand the fundamental difference between a thread and a process.
A process is a factory; a thread is a worker in the factory. All workers share the same warehouse (memory) but each works independently.
- thread
- An independent unit of execution within a process, sharing memory and file descriptor table with other threads, but with its own stack and registers.
- pthread
- POSIX Threads — the Unix threading standard, implemented on Linux via libpthread. The API includes pthread_create, pthread_join, and more.
- pthread_create
- Function to create a new thread. Takes a thread ID, attributes, start function, and argument. Returns 0 on success.
- pthread_join
- Waits for a thread to finish and releases its resources. Without join, a zombie thread remains in memory.
- TID (Thread ID)
- Unique identifier of a thread within a process, returned by pthread_self() or stored in the pthread_t passed to pthread_create.