17 Coordinating timelines

In this chapter
- Learn how to create a primitive to coordinate multiple timelines.
- Learn how functional programmers manipulate the two important aspects of time, ordering and repetition.
In the last chapter, we learned to diagnose resource-sharing bugs and created a concurrency primitive to share resources safely. Sometimes, multiple timelines need to work together when there’s no explicit resource they are sharing. In this chapter, we will build a concurrency primitive that will help timelines coordinate and eliminate incorrect possible orderings.
Principles of working with timelines
Here are the principles again as a reminder. In the last two chapters, we worked through principles 1–4, showing how they help ensure correctness. In this chapter, we will be applying principle 5. We need to begin thinking of time itself as something to command.
1. Fewer timelines are easier
Every new timeline dramatically makes the system harder to understand. If we can reduce the number of timelines (t in the formula on the right), it will help tremendously. Unfortunately, we often can’t control how many timelines we have.
2. Shorter timelines are easier
If we can eliminate steps in our timelines (decrease a in the formula on the right), we can reduce the number of possible orderings dramatically.
Formula for number of possible orderings

3. Sharing fewer resources is easier