One way to distribute a User Hashmap among distinct socket processing in Rust is to use shared mutable state through the Arc and Mutex constructs.
First, create an Arc and Mutex around the User Hashmap:
use std::sync::{Arc, Mutex};
let user_map = Arc::new(Mutex::new(HashMap::new()));
Then, in your socket processing threads, each thread can clone the Arc and Mutex to obtain a reference to the User Hashmap:
let shared_user_map = user_map.clone();
With this reference, the thread can then update or read from the User Hashmap as needed:
let mut user_map = shared_user_map.lock().unwrap();
user_map.insert(user_id, user);
Note that the .lock() method of the Mutex returns a locked data guard, which must be unwrapped first before accessing the data. Also note that this approach needs to be used with caution as shared mutable state can lead to data races and other issues.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-02-10 11:00:00 +0000
Seen: 9 times
Last updated: Jun 04 '21
What are the Cordapp Certificates used for in terms of signing and interacting?
How can SSL be used with CqlSessionFactoryBean in Springboot Cassandra?
How do I resolve a 502 error when attempting to call an HTTPS REST API from an HTTP REST API?
How can SSM parameters be accessed across multiple accounts?
How can compile-time errors be used to limit the possible permutations of a struct in Rust?
How can the SSL certificate verify error in Python be expressed differently?
What steps can I take to resolve the issue caused by GitHub's certificate/key change?
What causes my Rust program to perform differently based on the specific optimization level used?