Horje
rust•armanriazi•error•[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely `Rc<Mutex<i32>>` cannot be sent between threads safely Code Example
rust•armanriazi•error•[E0277]: `Rc>` cannot be sent between threads safely `Rc>` 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);




Rust

Related
rust error: failed to run custom build command for python3-sys Code Example rust error: failed to run custom build command for python3-sys Code Example
$sce trust url Code Example $sce trust url Code Example
rust•armanazi•lifetime•drop Code Example rust•armanazi•lifetime•drop Code Example
rust how to make print happen before asking for input Code Example rust how to make print happen before asking for input Code Example
rust•armanriazi•trait Code Example rust•armanriazi•trait Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12