Basic

The executor is the loop

A future does not run itself. Something calls it, checks whether it is ready, and calls it again, and that something is a component of the program with its own decisions.

What you will see

One line. Naming the executor names the fairness policy.

About 1 min of reading

Try It

Prove a wake-up is required

1

Write a poll that never wakes

printf 'poll -> not_ready wakeups 0\n' > /tmp/poll.txt; cat /tmp/poll.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. Zero wake-ups means the task is polled once and forgotten.
2

Add the wake-up

printf 'poll -> not_ready wakeups 1\n' > /tmp/poll2.txt; cat /tmp/poll2.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line with one wake-up. That is the difference between a hang and a run.
3

Name the executor

printf 'executor=multithread workers=4\n' > /tmp/ex.txt; cat /tmp/ex.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. Naming the executor names the fairness policy.

Data Flow

A task to a wake-up
1 The task is handed to the executor
↓
2 The executor polls the task
↓
3 The task reports not ready
↓
4 A wake-up is registered and the task is polled again
Why this works
Why the executor is chosen explicitly

Different executors have different policies. A program that does not name its executor is a program whose policy is inherited.

The one thing the executor cannot do

It cannot run a future that was never handed to it. Handing over is the contract, and the contract is the bug.

Examples

Ready or not yet

The executor asks the future to make progress. The answer is either work done or a wake-up scheduled.

A loop with no work is a bug

A future that returns not yet without registering a wake-up never runs again. The program hangs, and the cause is one line.

Fairness is a policy

Which task runs next is a choice. The choice is a policy, and the policy has a name.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Await compiles to a state machine
Previous Lesson
Await compiles to a state machine
Next Lesson
Cancellation loses work
Cancellation loses work →