Considering the following example:
pub trait Fusion: Send + Sync {
...
}
let ff2: Arc<RefCell<(Box<dyn Fusion>, bool)>> = ...;
all Impls of Fusion already has atomic, lock-free algorithm built-in, they are safe to be shared in multiple threads. It should be noted that both RwLock and Mutex cannot be used here as they are too inefficient.
However, ff2 (mutable reference counting) cannot be used in another thread, the compiler will complain:
error[E0277]: `RefCell<(Box<dyn Fusion>, bool)>` cannot be shared between threads safely
--> src/lib.rs:188:36
|
188 | let handle = thread::spawn(move || loop {
| ______________________-------------_^
| | ...
196 | | });
| |_________^ `RefCell<(Box<dyn Fusion>, bool)>` cannot be shared between threads safely
I need to find an alternative smart pointer that supports both reference counting and lock-free mutability. To my knowledge, this feature doesn't exist in vanilla Rust. What should be done in this case?