2025-10-26 · Technical Discussion
CPU Core / Process / Thread / Fiber / Coroutine
Introduction
A program consists of executable code or instructions, and the operating system brings together several concepts to execute programs efficiently. Understanding the following concepts can help you better understand how multitasking or concurrent programs work and how to optimize their performance:
- CPU Core
- Process
- Thread
- Fibers
- Coroutine
What Is a CPU Core?
A single CPU can contain multiple cores, each capable of executing computational instructions independently. More cores allow a computer to handle more work simultaneously, and a single program can also use multiple cores to perform more computations.
Physical Cores and Logical Cores
- Physical Core: Actual hardware within the CPU that performs computations.
- Logical Core: A technique that allows a physical core to execute instructions from different threads concurrently, making better use of CPU resources when they would otherwise sit idle. Imagine a factory working on tasks for production line B while waiting for raw materials to arrive at production line A, thereby achieving concurrency.
Process
When a program starts, the operating system allocates a separate memory space, state, and system resources to it. Each process is isolated from the others and does not interfere with them, which is why one application crashing does not cause other programs to crash.
Thread
A process can have one or more threads, and all its threads share the memory of that process.
Process (has its own memory resources) ├─ Thread 1 ├─ Thread 2 └─ Thread 3Although threads can share memory resources, this can also introduce race conditions. The following approaches help address them:
- Mutex: Protects a section of code so that only one thread can execute it at a time.
- Semaphore: A permit system with a limited number of permits, much like a parking lot counter.
- Atomic Operations: Guarantee that an entire read-modify-write operation cannot be interrupted.
- Message Passing: Avoids shared memory by passing messages between threads.
// “Do not communicate by sharing memory; share memory by communicating.”ch := make(chan int)go func() { ch <- 1 }()value := <-ch- Immutable Data: Rather than modifying existing data, creates new copies to work with.
- ……
Lighter-Weight Units of Execution
Although switching between threads is already much less expensive than switching between processes, thread overhead can still be too high in scenarios where tasks switch frequently but each task is very lightweight. To reduce this cost further, an approach emerged that implements execution units at the application level: instead of relying on operating system scheduling, the program controls its own execution flow. Common implementations include coroutines and fibers.
Coroutine
Think of a coroutine as “a function whose execution can be suspended and resumed.”
Unlike a regular function, a coroutine does not have to run to completion in one go. It can “yield control” during execution and later “resume where it left off.” This allows us to coordinate multiple tasks asynchronously within a single thread.
I essentially think of it as a JavaScript generator. The more familiar async await is, in effect, syntactic sugar for generators + Promises, allowing developers to write asynchronous flows in a synchronous style.
Fiber
Think of a fiber as “a coroutine supported by the operating system.”
Developers still need to control when switching occurs manually, explicitly specifying in the program when to yield control. Because this switching does not go through the operating system scheduler, it is extremely fast.
Implementations of the fiber concept vary considerably across languages and platforms.
Summary
- CPU Core: Determines how much work the hardware can perform simultaneously.
- Process: A container for a running program that provides a separate memory space.
- Thread: The unit that actually executes work within a program, sharing the resources of its process.
- Coroutines and Fibers: Enable lighter-weight task switching at the code level.
Further Reading
- CPU Cores VS Threads Explained - Max’s Tech
- Why Are Threads Needed On Single Core Processors - Core Dumped
- FANG Interview Question | Process vs Thread - ByteByteGo
- Cores vs. Logical Processors: What’s Really Powering Your CPU? - Ask Leo!
- Mutex vs Semaphore - geeksforgeeks
- Go by Example: Goroutines
- Goroutines explained - Awesome
- Coroutine: 入門篇 - electronic_blue
- 单线程并发 - Ray-D-Song’s Blog
- 协程(Coroutine)和纤程(Fiber) - Ray-D-Song’s Blog