#include #include volatile int global = 0; thread Poller {}; void main(Poller & this) { while(true) { waitfor( ^?{} : this ) { break; } or else { global = (global + 1) % 10; yield(); } } } thread Waiter; thread Waiter {}; void park_loop(Waiter & this, int id, bool force) { for(int i = 0; i < 5; i++) { // Unpark this thread, don't force a yield sout | id | "Calling unpark" | (force ? "(Force)" : "(No Force)") | i; unpark(this, force); // Force a preemption before the call to park int prev = global; while(prev == global) {} // Park this thread, sout | id | "Parking" | (force ? "(Force)" : "(No Force)") | i; park(); sout | id | "Unparked" | (force ? "(Force)" : "(No Force)") | i; } } volatile int count = 0; void main(Waiter & this) { // Get a unique id int id = __atomic_fetch_add(&count, 1, __ATOMIC_SEQ_CST); // First without forcing yield park_loop( this, id, false ); // First with forcing yield park_loop( this, id, true ); } int main() { Poller p; { Waiter waiters[5]; } }