Aviv
Aviv

Reputation: 19

golang hash-table with concurrency support

I need to use a very big hash-table, and access it from many readers and many writers in parallel. is there data structure like map, that support many reads and writes in parallel, without locking the whole structure each access?

Upvotes: 0

Views: 951

Answers (2)

manikawnth
manikawnth

Reputation: 3249

Since you asked for a map

without locking the whole structure each access

I direct you to the following implementation:

https://github.com/cornelk/hashmap

This project implements a pure lock free hash map data structure using atomic instructions common in many CPU architectures

The regular go sync.Map still uses an underlying Mutex which locks the corresponding map datastructure.

Upvotes: 1

Johnny
Johnny

Reputation: 9519

Package sync provides the concurrent safe map.

Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.


Although the spec itself point out these two specific cases when it should be used(otherwise they suggest using the normal map with locking mechanism):

  1. when the entry for a given key is only ever written once but read many times, as in caches that only grow
  2. when multiple goroutines read, write and overwrite entries for disjoint sets of keys

Upvotes: 0

Related Questions