Why is creating a Thread said to be expensive?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Creating a thread in a computing context refers to initializing a new path of execution within a program. While thread creation might seem trivial and lightweight in high-level abstractions, it's actually considered expensive due to multiple underlying reasons. This article will explore the technical details related to the cost of thread creation.
1. Thread Basics
Threads are a core component of modern computing, enabling programs to perform multiple operations concurrently. They provide a way to optimize processing across the hardware resources of a computer, particularly its multiple cores.
1.1. Understanding Threads
Threads share the process's resources such as memory and file descriptors, but they have their own stack, register, and program counter. The ability to share process data while having separate execution paths allows for efficient multitasking within applications.
2. The Cost of Thread Creation
Despite the benefits, thread creation is often termed "expensive" due to several factors:
2.1. Resource Allocation
Creating a new thread involves allocating stack memory for it, initializing various thread-specific data structures, and working with operating system (OS)-level handling to register the thread. This allocation is resource-intensive and can slow down the system's performance.
2.2. Context Switching
Threads require context switching, which involves storing and restoring the state of a CPU to resume thread execution. This adds overhead, as saving and loading the current execution state is costly in terms of time and CPU cycles.
2.3. Synchronization Overhead
When multiple threads are used, managing data consistency becomes complex, requiring synchronization mechanisms like mutexes and semaphores. Implementing these mechanisms incurs additional overhead, further amplifying the expense of thread management.
Related reading
- Why is creating a Thread said to be expensive?
- Why is DFS slower in one tree and faster in the other?
- Why is dictionary so much faster than list?
- Why is Dictionary.First so slow?
- Why is Facebook Javascript SDK loaded asynchronously?
- Why is i not atomic?
- Why is docker build taking so long to run?
- Why is faster than list?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.