Our first example demonstrates the use of tickers, which are used for performing actions repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.
In this Standard ML version, we simulate the behavior of channels and tickers using references and threads. The Time structure is used for timing operations.
We create a ticker that sends a value every 500 milliseconds. We use a separate thread to simulate the ticker’s behavior. Another thread is used to handle the ticks, printing the time of each tick.
The main thread sleeps for 1600 milliseconds, allowing about 3 ticks to occur, and then stops the ticker.
When we run this program, the ticker should tick 3 times before we stop it. The output might look something like this:
Note that Standard ML doesn’t have built-in support for concurrent operations like channels and tickers. This implementation is a simplified simulation of the concept using threads and shared mutable state, which isn’t idiomatic Standard ML. In a real Standard ML program, you would typically use a different approach to handle periodic events.