Guest User

Untitled

a guest
Mar 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #![feature(nll)]
  2.  
  3. #[macro_use]
  4. extern crate lazy_static;
  5.  
  6. use std::sync::Mutex;
  7. use std::collections::HashMap;
  8. use std::thread;
  9.  
  10.  
  11. struct Context {
  12. t: i32
  13. }
  14.  
  15. lazy_static! {
  16. // Since it's mutable and shared, use mutext.
  17. static ref CONTEXTS: Mutex<HashMap<thread::ThreadId, Context>> = Mutex::new(HashMap::new());
  18. }
  19.  
  20. fn get_context() -> &'static Context {
  21. let guard = CONTEXTS.lock().unwrap();
  22. let mut hashmap: HashMap<thread::ThreadId, Context> = *guard;
  23. let context = hashmap.get(&thread::current().id());
  24. match context {
  25. Some(context) => context,
  26. None => {
  27. let c = Context{t:99};
  28. hashmap.insert(thread::current().id(), c);
  29. &hashmap.get(&thread::current().id()).unwrap()
  30. }
  31. }
  32. }
  33.  
  34. fn main() {
  35. println!("{}", get_context().t);
  36. }
Add Comment
Please, Sign In to add comment