Lesson 5 of 7

Cores, Threads & Parallelism

Around 2005 single cores stopped getting much faster, so chips gained more cores instead. You'll learn how cores and threads work and why sharing between them needs care.

Clock speeds hit a power wall (~4 GHz produces too much heat to cool practically), so vendors put multiple full CPUs — cores — on one chip. Each core runs one instruction stream (thread) at a time; 8 cores genuinely execute 8 things simultaneously. Simultaneous multithreading (Intel's 'hyper-threading') lets one core juggle two threads to fill idle execution slots — helpful, but not double speed.

Text
One core, 3 GHz, in one second:  ~10 billion instructions
Eight cores: ~80 billion — IF the work splits evenly.

Amdahl's Law — speedup is capped by the serial fraction:
  90% parallel work, infinite cores -> at most 10x faster.
  50% parallel work, infinite cores -> at most 2x.

The catch: cores share memory. Two cores writing the
same data need coordination — and that's where bugs live.
C
// The classic data race:
// Two threads both run: counter++
// which is really THREE instructions:
//   load  counter into register
//   add   1
//   store register back to counter
//
// Interleaving:
//   Thread A loads 100      Thread B loads 100
//   A adds -> 101           B adds -> 101
//   A stores 101            B stores 101
// Two increments, counter went up by ONE. Lost update.
//
// Fixes: locks (mutex), atomic instructions
// (lock xadd on x86), or sharing nothing at all.
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.