mutex_replacement.rs (682B)
1 use std::sync::Arc; 2 3 use tokio::sync::Mutex; 4 5 // Experimenting with an actor replacement by using a simple mutex 6 pub async fn actor_replacement(state: Arc<Mutex<i64>>, value: i64) -> i64 { 7 let mut state = state.lock().await; 8 *state += value; 9 *state 10 } 11 12 // If we were to do something else while that process is happnening: 13 pub async fn actor_replacement_2(state: Arc<Mutex<i64>>, value: i64) -> i64 { 14 let update_handle = tokio::spawn(async move { 15 let mut state = state.lock().await; 16 *state += value; 17 *state 18 }); 19 // do something else by spawning another async task 20 // this will incur extra overhead 21 update_handle.await.unwrap() 22 }