Rafa Bacano
Rafa Bacano

Reputation: 11

How do i make to share a variable between threads in rust

Im currently making a server in rust, having a hashmap called player_list and it is always changing, players join, quit, etc and outside of the server code i wanted to make a coroutine to each three seconds check for connection for every player, and now im stuck in a place where i'm getting errors with the player_list hashmap because i cant make it shared between the main server code and this server coroutine that only works from 3 to 3 seconds.

let this be a small sample of the code:

let mut player_list: HashMap<SocketAddr, String> = HashMap::new();


tokio::spawn(async {
        loop {
            monitor_players(&player_list).await;
            sleep(Duration::from_secs(3));
        }
    });

loop {
//server code
}

Tried adding to this asyncronous task a function that is supposed to check for every player connection, sending each address a message and when it ends, sleep during 3 seconds but doesnt work and i cant see how to make the threads work with shared hashmap

Upvotes: 0

Views: 48

Answers (0)

Related Questions