surbhi
surbhi

Reputation: 397

In Go, can we synchronize each key of a map using a lock per key?

In Go, can we synchronize each key of a map using a lock per key? Is map level global lock always required? The documentation says that any access to map is not thread safe. But if a key exists, then can it be locked individually?

Upvotes: 3

Views: 2753

Answers (2)

Eagle
Eagle

Reputation: 509

This is a simple implementation of what you want: mapmutex.

Basically, a mutex is used to guard the map and each item in the map is used like a 'lock'.

Upvotes: 1

nothingmuch
nothingmuch

Reputation: 1516

Not exactly, but if you are only reading pointers off a map and modifying the referents, then you aren't modifying the map itself.

Upvotes: 1

Related Questions