This is a C++ project that simulates the women's 4x100m relay from the Tokyo 2020 Olympics. There are four teams of four runners, so sixteen runners in total, and each runner is a separate thread. The threads are kept in step using mutexes, two barriers, condition variables and an atomic flag. While the race runs it also sends every event over the network to a second program, which shows communication between two separate processes.
The repository also holds a set of small Python scripts that measure and compare the communication overhead of threads, udp and WebSockets.
The C++ side is made up of two programs.
relay_race.cppis the simulation itself. It runs the whole race with threads and sends each event over udp. The events include a runner getting ready, starting, taking the baton, finishing a leg, fumbling or dropping the baton, and the winning team.viewer.cppis a small udp client. It listens on port 9000 and prints every event the race sends to it.
- A
std::mutexwithstd::lock_guardguards the shared random number generator, the console output, the udp socket and the running team totals, so many threads can use them safely. - The barrier in
barrier.hppholds all sixteen runner threads and the main thread until everyone has reached the same point, then lets them all go at once. - A
std::condition_variableon each runner is how the baton is passed. A runner waits on the previous runner and only starts once that runner has finished. - A
std::atomic<bool>is used for the winner flag. Several threads can finish at almost the same moment, and the atomic makes sure only one of them is named the winner. - udp sockets through the Asio library send the race events to the viewer program.
maincreates one thread per runner. Each thread prints that its runner is ready.- Every runner thread and the main thread wait at the first barrier, so the race cannot start until all sixteen runners are on the blocks.
- The main thread waits a random one to three seconds, then releases everyone at the second barrier and prints GO.
- Inside a team the four legs run in order. Each runner waits for the previous runner to hand over the baton before starting, and each leg takes a random ten to twelve seconds.
- The last leg of each team has a small random chance to fumble the baton, which adds a delay, or to drop it, which disqualifies the team.
- The first team to finish its last leg wins. The atomic winner flag makes sure only that first team prints the winning line.
- Once every thread has finished, the total time for each team is printed.
Every one of these events is also sent over udp, so if you run the viewer next to the race you see the same commentary arrive in the second program at the same time.
relay_race.cpp, the relay race simulation and main program.viewer.cpp, the udp client that prints the race events.threads_test.py, times message passing between two threads in one process.udp_server.pyandudp_client.py, time message passing over udp on localhost.ws_server.pyandws_client.py, time message passing over WebSockets on localhost.Makefile, builds both C++ programs..gitignore.README.md, this file.
A few files are needed to compile the project but are not my own work, so they are not included here. Put them in the project folder before you build.
asio.hppand theasio/folder. This is Asio 1.24.0, a header only networking library used for the udp sockets. You can get it from https://github.com/chriskohlhoff/asio, where the headers are underasio/include/, or install it withbrew install asioand point the include path at it.cs_helper_DoNotModify.hpp, a helper header that holds theTeamandCompetitorclasses the race uses.barrier.hpp, a small reusable thread barrier, used because C++11 does not include<barrier>.
With those in place the project folder should look like this.
asio.hpp
asio/
cs_helper_DoNotModify.hpp
barrier.hpp
relay_race.cpp
viewer.cpp
threads_test.py
udp_server.py
udp_client.py
ws_server.py
ws_client.py
Makefile
README.md
You need a C++17 compiler such as g++ or clang++ and POSIX threads through -pthread. Once the files above are in place, run.
make # builds ./race and ./viewer
make clean # removes the built programsOr build them by hand.
g++ -std=c++17 -pthread -I. relay_race.cpp -o race
g++ -std=c++17 -pthread -I. viewer.cpp -o viewerThe viewer needs to be listening before the race starts, so use two terminals.
Start the viewer first.
./viewerThen start the race in the other terminal.
./raceThe race prints its own commentary, and the viewer prints the same events as they arrive over udp. A full run takes about 45 to 50 seconds. Here is some example output.
Re-run of the women's 4x100 meter relay at the Tokyo 2020 Olympics.
Williams ready, Thompson-Herah ready, ...
The race official raises her starting pistol...
GO !
Leg 1: Williams ran in 11.51 seconds. (Jamaica)
Thompson-Herah (Jamaica) took the baton from Williams (Jamaica)
...
Thomas FUMBLED the baton! Team United States is delayed by 0.63 seconds.
...
Team Switzerland is the WINNER!
TEAM RESULTS
Team Jamaica = 47.02 s
Team United States = 44.63 s
Team Great Britain = 44.29 s
Team Switzerland = 44.17 s
The Python scripts compare three ways of getting a message from a sender to a receiver and measure how fast each one is. Two of them, threads and udp, are the same methods the race itself uses.
The idea is the same for all three. The sender sends a tiny message, the receiver sends it straight back, and the sender times how long the round trip took. Round trip time just means the time from sending the message to getting it back. The test repeats this ten thousand times and takes the average, so one slow moment does not throw off the result. The one way latency is half of the round trip, because the message travels the distance twice.
What the tests really measure is overhead. Overhead is the extra time added by everything that sits between the sender and the receiver. The more machinery a message has to pass through, the longer it takes.
threads_test.pystarts two threads inside one program and passes a signal between them with twothreading.Eventobjects. Both threads share the same memory, so nothing has to leave the program. This is the fastest because there is almost nothing in the middle.udp_server.pyandudp_client.pyare two separate programs. The client sends the message to the server over a udp socket on the same machine, and the server sends it straight back. The message now has to go through the operating system, so this is slower than threads.ws_server.pyandws_client.pyare also two separate programs, talking over a WebSocket on the same machine. A WebSocket runs on top of TCP and wraps every message in extra layers, so this is the slowest of the three.
In the udp and WebSocket tests the server just waits for a message and sends it straight back, while the client does the sending and the timing. The thread test does not need a server because both threads are already inside the same program.
The thread test runs on its own.
python3 threads_test.pyThe udp and WebSocket tests each need their server running first, so use two terminals. The WebSocket scripts also need the websockets package.
pip install websockets
# udp, one terminal each
python3 udp_server.py
python3 udp_client.py
# WebSockets, one terminal each
python3 ws_server.py
python3 ws_client.pyEach client prints the average round trip time and the one way latency when it finishes.
A typical run gives the results below, in microseconds.
- Threads sharing memory were fastest, at about 7.20 microseconds round trip and 3.60 microseconds one way.
- udp on localhost was in the middle, at about 15.51 microseconds round trip and 7.76 microseconds one way.
- WebSockets on localhost were the slowest, at about 73.99 microseconds round trip and 37.00 microseconds one way.
So the overhead goes up as the communication moves from shared memory between threads, to the operating system with udp, and then to the extra protocol layers that WebSockets add on top of TCP.