Ernestas Poškus

Technical blog

"We must view with profound respect the infinite capacity of the human mind to resist the introduction of useful knowledge." - Thomas R. Lounsbury

| github | goodreads | linkedin | twitter |

ansible 2 / elasticsearch 2 / kernel 2 / leadership 1 / linux 2 / mnemonics 1 / nginx 1 / paper 40 / personal 5 / rust 1 / tools 2 /

Cooperative Task Management without Manual Stack Management

WC 250 / RT 2min


Or, Event-driven Programming is Not the Opposite of Threaded Programming

Two programming styles as a conflation of two concepts: task management and stack management.

Those two concerns define a two-axis space in which ‘multithreaded’ and ‘event-driven’ programming are diagonally opposite; there is a third ‘sweet spot’ in the space that combines the advantages of both programming styles.

Different task management approaches offer different granularities of atomicity on shared state. Conflict management considers how to convert available atomicity to a meaningful mechanism for avoiding resource conflicts.

High-performance programs are often written with preemptive task management, wherein execution of tasks can interleave on uniprocessors or overlap on multiprocessors.

The opposite approach, serial task management, runs each task to completion before starting the next task. Its advantage is that there is no conflict of access to the shared state; one can define inter-task invariants on the shared state and be assured that, while the present task is running, no other tasks can violate the invariants.

A compromise approach is cooperative task management. In this approach, a task’s code only yields control to other tasks at well-defined points in its execution; usually only when the task must wait for long-running I/O. The approach is valuable when tasks must interleave to avoid waiting on each other’s I/O, but multiprocessor parallelism is not crucial for good application performance.

Notes

Preemptive - wherein execution of tasks can interleave on uniprocessors or overlap on multiprocessors.

Serial task management, runs each task to completion before starting the next task.