Skip to content
BENIBIX Learn

Thread

Development⚙️ Compilers & RuntimesConcurrency

Also known as Threads, Thread of Execution, Multithreading

One line of work in a running program. It shares the program's memory.

Think of it like

Think of it like cooks sharing one kitchen. They work at the same time, using the same pots.

Example

Jack's photo app uses one thread to keep the screen moving. A second thread shrinks 500 photos in the background, so the app never freezes.

Why it matters

Threads let one app do several things at once and use more than one CPU core.

Definition

A thread is one line of work running inside a process. A process starts with one thread and can start more. All threads in a process share its memory, so they can use the same data. That makes them cheaper to start than processes and quick to share work. It also means two threads can change the same data at once, which causes bugs.

Common mistake

Letting two threads change the same data with no lock. The result changes from run to run, and the bug is hard to catch.

Key takeaways

  1. 1
    Inside a process. One or many.
  2. 2
    Shared memory. Same data for all.
  3. 3
    Needs care. Locks around shared data.

Compare