How Threads Actually Work Under the Hood

I asked how threads actually work under the hood… and the answer was more interesting than I expected. After my last post, I got some really insightful explanations about how threads are scheduled. Here’s what finally started to make things click for me: -> Threads are NOT directly tied to CPU cores. Instead: • The OS scheduler decides which thread runs • A thread can run on different cores at different times • Threads don’t “own” a core — they just get time on it What actually happens The CPU doesn’t run multiple threads magically. It does one of two things: 1. Parallel execution (multiple cores) Different threads can run at the same time on different cores. 2. Time slicing (single core or oversubscription) The CPU rapidly switches between threads. The important part: context switching When switching between threads, the CPU: • Saves the current thread state (registers, instruction pointer, etc.) • Loads another thread’s state • Continues execution So it’s not “running everything at once” — it’s switching fast enough to look like it is. What surprised me A thread: • can move between cores • doesn’t execute continuously • depends completely on the OS scheduler So writing multithreaded code means: 👉 You cannot assume order 👉 You cannot assume timing 👉 You cannot assume which core runs your code What I realized Multithreading is not just about creating threads. It’s about writing code that works no matter how unpredictably those threads are scheduled. Still exploring this space — next I’m trying to understand: How synchronization primitives (mutex, locks) actually work internally #cpp #multithreading #systems #concurrency #learning #os #softwareengineering

You’re gonna have a fun time with those primitive, but some hints. The kernel uses spinlocks to synchronize between different cores (and different physical threads, on a hyper threaded CPU). Those are used to implement other primitives like mutex, semaphore, rwlock, condition variables etc. that the user program uses. On Linux there is a funny primitive called futex which is a funny combo between spinlock and mutex, intended to get good performance in all cases, but a bit more complex to implement (not necessarily to use)

Very insightful, could you please post the source of this or any promt if you used LLM. WOULD LIKE TO READ MORE IN THIS.

See more comments

To view or add a comment, sign in

Explore content categories