FAQ
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.

Search Discussions

  • 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.
  • Peter Volpe at Mar 24, 2014 at 3:51 pm
    self/s was a typo. Thanks for your help.
    On Friday, March 21, 2014 11:21:28 PM UTC-7, Henrik Johansson wrote:

    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 <peter...@ff0000.com <javascript:>>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...@googlegroups.com <javascript:>.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 22, '14 at 5:00a
activeMar 24, '14 at 3:51p
posts3
users2
websitegolang.org

2 users in discussion

Peter Volpe: 2 posts Henrik Johansson: 1 post

People

Translate

site design / logo © 2022 Grokbase