Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage for counters #18

Open
stgleb opened this issue Apr 5, 2016 · 2 comments
Open

Usage for counters #18

stgleb opened this issue Apr 5, 2016 · 2 comments
Assignees
Labels

Comments

@stgleb
Copy link

stgleb commented Apr 5, 2016

Hi, folks.

I've decided to use your CHM implemendation. But i have specifical use case. I use
map for counting, actually freqeuency of words. So you guarantee synchronization of each particular
operation. How can i reach consistency when i

  1. read value from map
  2. increment it
  3. write it back to map

Of source i can use my own global lock, but it will totaly destoys idea of map concurrency.
Shoud i take ConcurrentShard and use it internal RWLock?

@stgleb
Copy link
Author

stgleb commented Apr 5, 2016

i've used such code

for i := 0; i < len(words); i++ {
        shard := wordsMap.GetShard(words[i])
        shard.RWMutex.Lock()
        count, ok := wordsMap.Get(words[i])

        if ok != true {
            count = 0
        } else {
            count += 1
        }
        wordsMap.Set(words[i], count)
        shard.RWMutex.Unlock()
    }

Could you tell me i'm on the right way?

@wgjak47
Copy link

wgjak47 commented Aug 27, 2019

For int counter, I thinks use Atomic is a good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants