Answers for "rust•armanriazi•error•[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely `Rc<Mutex<i32>>` cannot be sent between threads safely"

0

rust•armanriazi•error•[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely `Rc<Mutex<i32>>` cannot be sent between threads safely

The compiler is also telling us the reason why: the trait `Send` is not implemented for `Rc<Mutex<i32>>` . We’ll talk about Send in the next section: it’s one of the traits that ensures the types we use with threads are meant for use in concurrent situations.
Unfortunately, Rc<T> is not safe to share across threads. When Rc<T> manages the reference count, it adds to the count for each call to clone and subtracts from the count when each clone is dropped. But it doesn’t use any concurrency primitives to make sure that changes to the count can’t be interrupted by another thread. This could lead to wrong counts—subtle bugs that could in turn lead to memory leaks or a value being dropped before we’re done with it. What we need is a type exactly like Rc<T> but one that makes changes to the reference count in a thread-safe way.
let counter = Arc::new(Mutex::new(0));    
let counter = Arc::clone(&counter);
Posted by: Guest on March-25-2022

Code answers related to "rust•armanriazi•error•[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely `Rc<Mutex<i32>>` cannot be sent between threads safely"

Browse Popular Code Answers by Language