These are chat archives for rust-lang/rust

15th
Dec 2017
James McCoy
@jamessan
Dec 15 2017 00:55
@cjmochrie https://play.rust-lang.org/?gist=b38f2ea84c66d49f64241a980035aca9&version=stable will. You may also want to use map.entry(...).or_insert(0) or something along those lines.
Cameron Mochrie
@cjmochrie
Dec 15 2017 01:06
Ahh, I like that
Thanks @jamessan !
red75prime
@red75prime
Dec 15 2017 05:08
@cjmochrie , You can also replace map.get("foo").unwrap() with map["foo"]
Sherab Giovannini
@Shaddy
Dec 15 2017 12:26
guys, I want to have a shared reference to a hashmap, between some threads, my idea is to just have it as read access, and mutation of that hashmap will ocurr as initialization. Which is the best approach? Arc?
Michal 'vorner' Vaner
@vorner
Dec 15 2017 12:48
@Shaddy Arc sounds like the default option there, yes.
Sherab Giovannini
@Shaddy
Dec 15 2017 12:52
and, would I be able to mutate the hashmap after it being shared to threads?
it should be syncd with some sync guess
Michal 'vorner' Vaner
@vorner
Dec 15 2017 12:52
No.
If you mutate it first and then stuff into Arc, then it works. But afterwards, you can't change it.
If you want to change it later, use Arc<Mutex<HashMap<_, _>>>
(Or RWLock or something similar)
Zakarum
@omni-viral
Dec 15 2017 12:53
You can change it if you drop all except one Arcs
And unwrap from last Arc
Or use get_mut
on last one
Sherab Giovannini
@Shaddy
Dec 15 2017 13:05
the thing is that during initialization hashmap would be empty, and there is a gap between threads are running and the "stuff" really starts, where the hashmap would be filled with the content, so I think that Arc<Mutex<HashMap<_, _>>> or RWLock should be the candidate
ty
Sean
@seanr707
Dec 15 2017 15:21

Windows build finally successful :smile:

GitterGTK - Win64

Sherab Giovannini
@Shaddy
Dec 15 2017 16:10
wow! nice!
is it any way to wait for the threads at drop?
James McCoy
@jamessan
Dec 15 2017 16:32
https://play.rust-lang.org/?gist=915b7ebebcbaded9783202cc61a0099b&version=stable seems to do it. Not sure if there's a more idiomatic way
Sherab Giovannini
@Shaddy
Dec 15 2017 16:33
nice approach!
thanks for the tip <3
Sherab Giovannini
@Shaddy
Dec 15 2017 16:40
what I didnt know is what was the problem, why I couldn't do it a Drop but I could do at any moment
Sherab Giovannini
@Shaddy
Dec 15 2017 20:06

Is there a way to safely pass this reference to threads?

type CallbackMap = Arc<RwLock<HashMap<u64, &'static Fn(bucket::Interception) -> Action>>>;