r/Compilers • • Jun 25 '26

Checking my understanding about threads, hyper-threading and concurrent and parallel programming

Hello, so after a while i've been tring to figure out concurrent and parallel programming, if anyone would kindly check my understanding if what I am saying is correct or not.

So i'm sure this is not 100% and, only like 70-80% or maybe even less but, here it goes:

So software threads are loaded into hardware threads which is managed by the cpu, they get multiplexed aka actual parallelism, executing two pieces of codes per core at the same time. after 20-30 milliseconds, the OS again does context switch by loading another software threads into the hardware threads (i think one by one or multiples at once depending on how hardware threads per cores ??), to simulate fake parallelism of multiples programs running.

Now for coroutines aka low level async await and, any other form of concurrent programming, i assume they use threads behind the scenes, maybe depending on the programmimng language i'm not sure, but they recycle the same main thread of the program so it stays at 100% usage, no idling.

I know that most implementations of generators and promises/futures use coroutines in some way.

Is this okay for now ? Any other good resources that teach these stuff from the ground up, in an ordered manner if possible ?

20 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jun 25 '26

So if I have 12 hardware threads, the OS will context switch 12 software threads into 12 hardware threads at oncr, and they will be executed in parallel (the 12 software threads on the hardware threads) is that right ? Or there is always exceptions ?

For coroutines, basically it will call other coroutines if that coroutine will be interrupted ? (Doing a heavy task, blocking operation) how do other coroutines know that they finished the blocking processing operation for each other ?

3

u/FloweyTheFlower420 Jun 26 '26

So if I have 12 hardware threads, the OS will context switch 12 software threads into 12 hardware threads at oncr, and they will be executed in parallel (the 12 software threads on the hardware threads) is that right ? Or there is always exceptions ?

Yes, provided the 12 software threads are actually schedule-able.

For coroutines, basically it will call other coroutines if that coroutine will be interrupted ? (Doing a heavy task, blocking operation) how do other coroutines know that they finished the blocking processing operation for each other ?

Generally, you put the callback into a completion queue and then your main event loop will "pump" the coroutines by calling continuations once the relevant IO returns a result. This concept is shared between stackless and stackful coroutines. A fairly illustrative example would be to implement stackful coroutines with only one primitive yield(), which will yield the current fiber. You should be able to spawn fibers, and then yield should switch the next available fiber, if it exists. You should think about what your context switch primitive would need to do: it's a (c compatible) function, so think in terms of the calling convention. You also need to switch the stack.

1

u/[deleted] Jun 26 '26

Ah okay, do you have any resources to learn these type of stuff in detail ?