In this recipe, we will create a very simple simulation:
- Run cargo new black-white to create a new application project and open the directory in Visual Studio Code.
- Open src/main.rs to add some code. First, we are going to need some imports and an enum to make our simulation interesting:
use std::sync::{Arc, Mutex};use std::thread;use std::time::Duration;////// A simple enum with only two variations: black and white/// #[derive(Debug)]enum Shade { Black, White,}
- In order to show a shared state between two threads, we obviously need a thread that works on something. This will be a coloring task, where each thread is only adding white to a vector if black was the previous element and vice versa. Thus, each thread is required ...