drgzmmy6379 2018-08-04 17:38
浏览 40
已采纳

Golang RWMutex在地图内容上的编辑

I'm starting to use RWMutex in my Go project with map since now I have more than one routine running at the same time and while making all of the changes for that a doubt came to my mind.

The thing is that I know that we must use RLock when only reading to allow other routines to do the same task and Lock when writing to full-block the map. But what are we supposed to do when editing a previously created element in the map?

For example... Let's say I have a map[int]string where I do Lock, put inside "hello " and then Unlock. What if I want to add "world" to it? Should I do Lock or can I do RLock?

  • 写回答

2条回答 默认 最新

  • duanqiao1947 2018-08-04 18:10
    关注

    You should approach the problem from another angle.

    A simple rule of thumb you seem to understand just fine is

    You need to protect the map from concurrent accesses when at least one of them is a modification.

    Now the real question is what constitutes a modification of a map.

    To answer it properly, it helps to notice that values stored in maps are not addressable — by design. This was engineered that way simply due to the fact maps internally have intricate implementation which might move values they contain in memory to provide (amortized) fast access time when the map's structure changes due to insertions and/or deletions of its elements.

    The fact map values are not addressable means you can not do something like

    m := make(map[int]string)
    m[42] = "hello"
    go mutate(&m[42]) // take a single element and go modifying it...
    // ...while other parts of the program change _other_ values
    m[123] = "blah blah"
    

    The reason you are not allowed to do this is the insertion operation m[123] = ... might trigger moving the storage of the map's element around, and that might involve moving the storage of the element keyed by 42 to some other place in memory — pulling the rug from under the feet of the goroutine running the mutate function.

    So, in Go, maps really only support three operations:

    • Insert — or replace — an element;
    • Read an element;
    • Delete an element.

    You cannot modify an element "in place" — you can only go in three steps:

    1. Read the element;
    2. Modify the variable containing the (read) copy;
    3. Replace the element by the modified copy.

    As you can now see, the steps (1) and (3) are mere map accesses, and so the answer to your question is (hopefully) apparent: the step (1) shall be done under at least an read lock, and the step (3) shall be done under a write (exclusive) lock.


    In contrast, elements of other compound types — arrays (and slices) and fields of struct types — do not have the restriction maps have: provided the storage of the "enclosing" variable is not relocated, it is fine to change its different elements concurrently by different goroutines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀