Reputation: 10095
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?
Upvotes: 5
Views: 4179
Reputation: 109347
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.
Upvotes: 4