SHARE
TWEET

Untitled

a guest Jun 18th, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::rc::Rc;
  2. use std::sync::{Mutex, Arc};
  3. use std::thread;
  4.  
  5. /////////// some module providing some data tree containing RCs
  6. #[derive(Debug)]
  7. struct S1 {
  8.     hai: i32,
  9. }
  10.  
  11. #[derive(Debug)]
  12. struct S2 {
  13.     s1: Rc<S1>,
  14. }
  15.  
  16. // multithreaded application using the data tree from multiple threads
  17. // this cannot work because every thread could clone() and hold the rc,
  18. // breaking the rc?
  19. //
  20. // is this bad design? how to fix?
  21. //  * &'s and lifetimes? (failed two times already...)
  22. //  * use Arcs in the "third party" module
  23. //    note; I've done this (the "third party" is also me) and it works
  24. //
  25. //
  26.  
  27. fn main() {
  28.     let s2 = Arc::new(Mutex::new(S2{s1: Rc::new(S1{hai: 3})}));
  29.    
  30.     let t = thread::spawn(move || { dbg!(s2.clone()) });
  31.     t.join();
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top