Visualized multi-threaded simulators in Ruby
We all do simulations in our heads, so why not make one in Ruby? Something that we can observe, fast forward, or let it linger. Let’s see how custom shared timers, threads, queues, and mutexes work together, and how they can form a petrol station with optimal cost and throughput.
The cars are coming to a station. They want to get some fuel. They come at different times and they need to be served. Just like web requests but this time we’ll not do web development. This we do at work. So for fun we want to build something that feels more alive. A small closed system that we could fully control and derive further ideas from. This is how we start thinking about simulators.
The main idea of a simulation is that it happens in some programmable environment. First, we need to think about how to control time. If every piece of the simulation, like a car or a station, runs in its own thread, then how do we synchronize them? How to handle access into shared resources? What data structures should we use?
Also, how do we create a protocol for gathering results to help us make decisions? How do we write a config file for a simulator engine that can spawn multiple scenarios in parallel? And last but not least, how do we visualize a simulation?
This presentation is a pure Ruby feast. We will operate on MRI and visualize the simulation in two ways: with d3.js, and through ASCII art. See you there! 🚕