|
|
|
|
|
by hknmtt
973 days ago
|
|
iirc RWMutex used to be much slower than standard Mutex so there was no good use case for it over standard Mutext. That might have gotten fixed, don't know. Secondly, you can use atomics instead of mutexes. They will be faster by about 400% iirc. Thirdly, you do not need to be concerned for readers, only for writers. Readers can concurrently read and they do not care if value changes. The only concern is writers because the can collide and that is where you need synchronizing. Protecting reads makes sense only in rare cases where you might have a map and it gets set to nil. So obviously reading from nil map will panic. Other than that there is not really much cases to protect readers and add locking overhead just for them. |
|
Whether atomics are applicable depend heavily on the structures and what you are doing under read lock. Atomics are good for reading primitives or pointers that get swapped whole, but not very useful for reason complex structures or traversing arrays/maps. Lock-free atomic structures exist, but are also often slower than their non-atomic counterparts.
Rather than making generic statements about what is slow and what is best, profile, profile, profile!