Firstly, forgive me if this is a stupid question. I would like to create a generic synchronised list (like in Java) for reuse in my Go projects. I found the source of Go's linked list and I was wondering would it be sufficient to simply add mutex locks to the list manipulation functions?
1条回答 默认 最新
- dongnaigu2052 2014-12-22 20:36关注
If you're going to make a concurrent-safe container, you need to protect all access to the data, not just writes. Inspecting an element, or even calling
Len()
without synchronizing the read could return invalid or corrupt data.It's probably easier to protect the entire data structure with a mutex, rather than implement your own concurrent linked list.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报