Skip to main content

Thread

Pronunciation
THRED
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/thread

In short

A thread is the smallest unit of execution an operating system can schedule, running inside a process and sharing that process's memory with other threads.

What is a thread in programming?

A thread is a single sequence of instructions that the CPU executes. Every process starts with one main thread, and a program can create more threads so that several tasks make progress at the same time, such as downloading a file while keeping the user interface responsive.

Each thread has its own stack, program counter, and register values, but all threads in a process share the same heap memory, global variables, and open files. The operating system scheduler switches between threads, and on a multi-core CPU several threads can run truly in parallel. Because threads share memory, two threads updating the same data at once can cause a race condition, so programs use locks, mutexes, and other synchronization tools to coordinate access.

Think of a process as a shared kitchen and threads as the cooks working in it. The cooks share the same fridge and counter, which makes cooperation fast, but they must take turns with the one sharp knife or they get in each other's way. Threads are used in web servers handling many requests, games that separate rendering from physics, and apps doing work in the background.

Threads are often confused with processes and with asynchronous code. A process has its own isolated memory, while threads inside a process share memory. Asynchronous programming with an event loop, as in Node.js, can juggle many tasks on a single thread by switching between them while they wait for I/O, instead of running them in parallel.

Key takeaways

  • A thread is a unit of execution that lives inside a process.
  • Threads in the same process share memory, but each has its own stack.
  • Multiple threads can run in parallel on a multi-core CPU.
  • Shared memory makes race conditions possible, so synchronization such as locks is needed.
  • Threads are cheaper to create and switch between than processes.

Example

Running work in several threads in Pythonpython
import threading

def worker(name):
    print(f"Thread {name} is running")

threads = [threading.Thread(target=worker, args=(i,)) for i in range(3)]

for t in threads:
    t.start()  # Begin running worker() in a new thread

for t in threads:
    t.join()   # Wait for each thread to finish

print("All threads done")

Readers ask

What is multithreading?

Multithreading is when a single program uses multiple threads to perform tasks concurrently. It improves responsiveness and can speed up work on multi-core CPUs, but it requires care to avoid race conditions and deadlocks.

Is a thread faster than a process?

Creating and switching between threads is usually cheaper than doing the same with processes, because threads share memory and resources. Processes, however, offer stronger isolation, so a crash in one does not bring down the others.

What is a race condition?

A race condition happens when a program's result depends on the unpredictable timing of threads accessing shared data. It is usually prevented with locks or atomic operations so that only one thread changes the data at a time.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings