Skip to main content
Engineering LibreTexts

9: Threads

  • Page ID
  • When I mentioned threads in Section 2.3, I said that a thread is a kind of process. Now I will provide a more careful explanation.

    When you create a process, the operating system creates a new address space, which includes the text segment, static segment, and heap; it also creates a new “thread of execution”, which includes the program counter and other hardware state, and the call stack.

    The processes we have seen so far are “single-threaded”, which means that only one thread of execution runs in each address space. In this chapter, you will learn about “multi-threaded” processes that have multiple threads running in the same address space.

    Within a single process, all threads share the same text segment, so they run the same code. But different threads often run different parts of the code.

    And they share the same static segment, so if one thread changes a global variable, other threads see the change. They also share the heap, so threads can share dynamically-allocated chunks.

    But each thread has its own stack, so threads can call functions without interfering with each other. Usually threads don’t access each other’s local variables (and sometimes they can’t).

    The example code for this chapter is in the repository for this book, in a directory named counter. For information on downloading this code, see Section 0.2.

    • Was this article helpful?