|
Henrik Johansson |
at Mar 22, 2014 at 6:21 am
|
⇧ |
| |
You don't appear to be locking the same lock but perhaps it is a typo (self
-> s)?
Other than that I think it should work as expected unless the rest of the
code does something else.
As long as the shared part is protected by the same lock that kind of swap
should be ok.
Garbage collection of the map might not happen _directly_ upon exit from
Rotate but at some time after.
On Sat, Mar 22, 2014 at 3:20 AM, Peter wrote:Is it safe in go to replace a map variable ie:
func (s *TestStruct) Add(k string, v string) {
s.Lock()
s.m[k] = v
s.Unlock()
}
func (s *TestStruct) Rotate() {
self.Lock()
oldMap := s.m
self.m = make(map[string]string)
self.Unlock()
for k, v := range oldMap {
....
}
}
Is it safe to assume that any changes made by Add will always go to the new map and that oldMap and all it's contents will be garbage collected after Rotate is finished?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout. --
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.