Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(nll)]
- #[macro_use]
- extern crate lazy_static;
- use std::sync::Mutex;
- use std::collections::HashMap;
- use std::thread;
- struct Context {
- t: i32
- }
- lazy_static! {
- // Since it's mutable and shared, use mutext.
- static ref CONTEXTS: Mutex<HashMap<thread::ThreadId, Context>> = Mutex::new(HashMap::new());
- }
- fn get_context() -> &'static Context {
- let guard = CONTEXTS.lock().unwrap();
- let mut hashmap: HashMap<thread::ThreadId, Context> = *guard;
- let context = hashmap.get(&thread::current().id());
- match context {
- Some(context) => context,
- None => {
- let c = Context{t:99};
- hashmap.insert(thread::current().id(), c);
- &hashmap.get(&thread::current().id()).unwrap()
- }
- }
- }
- fn main() {
- println!("{}", get_context().t);
- }
Add Comment
Please, Sign In to add comment