CPU Threads and Cores

What is CPU Threads and Cores?

Imagine a busy road with multiple lanes, each lane can handle a certain number of cars. This road handling cars is like CPU handling tasks and operations in computer i.e., Road = CPU.

What is a core in CPU? (Lanes on Road, Cores in CPU)
  • The lanes on the road are like the cores in a CPU i.e., Lanes on Road = Cores in CPU.
  • Just like multiple lanes allow multiple cars to move parallelly on the road, each core allows multiple tasks and operations to run parallel in computer at once.
What is a thread in CPU? (Cars on Road, Threads in CPU)
  • AĀ threadĀ refers toĀ smallest sequence ofĀ instructionsĀ which are executedĀ in CPU i.e., tasks or instructions = threads.
  • The cars on the road are like threads i.e., cars on Road = threads in CPU.
  • Just like multiple cars can travel on a road with multiple lanes, multiple threads can be executed by CPU with multiple cores.

For example,

  • if a CPU has 1 core, it’s like having 1 lane on the road. It can only run 1 thread at a time i.e.,
    • 1-core CPU can handle 1-thread at a time.
    • 1-lane Road can handle 1-car at a time.
  • if a CPU has 4 core, it’s like having 4 lane on the road. It can only run 4 thread at a time i.e.,
    • 4-core CPU can handle 4-threads at a time.
    • 4-lane Road can handle 4-cars at a time.

Each core can work on a different task, making the computer faster and more efficient.

How do CPU handle multiple threads with limited cores?

When computers need to execute multiple tasks simultaneously but have limited cores, then CPU uses “context switching”.

Context Switching: Handling Multiple Threads

Imagine a receptionist at a busy office.

  • While attending to a visitor, the phone rings, and the receptionist needs to pause the current task (greeting the visitor) to answer the call.
  • This is similar to context switching in a computer.
What is Context Switching in CPU?
  • In a computer, context switching works just like the multi-tasking receptionist.
  • When the CPU is working on thread 1 and thread 2 comes in, it pauses the current thread, saves its progress, and switches to the new thread.
How does context switching helps in handling multiple threads?

Context switching helps CPU to handle multiple threads keeping the computer responsive and capable of handling a multiple operations at a time.