There is plenty of complexity involved in letting threads move from one CPU core to the next, which I didn't understand. If a thread is waiting on something, it's removed from the schedulers.

When a kernel operation blocks, or the clock ticks, the scheduler is called to switch threads.

Most (not all) of the schedulers are "Min Heaps", which is a datastructure which always pops it's smallest item by some measure. (There's also max heaps which are implemented basically the same way)

2/3

Adrian Cochrane@alcinnz@floss.socialTo implement a min heap you build a binary tree where each "branch" holds a value smaller than either of it's children. Then store it in an array where index "i" has children 2i & 2i+1.

Add a value, first place it at the end of the array. Then swap it with it's parent until the invariant is reasserted.

To remove the smallest value (which is at the root), replace it with the last value removed from the end of the array and swap it with a child until the invariant is reasserted.

3/3