go - Golang: Best way to read from a hashmap w/ mutex -


this continuation here: golang: shared communication in async http server

assuming have hashmap w/ locking:

//create async hashmap inter request communication type state struct {     *sync.mutex // inherits locking methods     asyncresponses map[string]string // map ids values } var state = &state{&sync.mutex{}, map[string]string{}} 

functions write place lock. question is, best / fastest way have function check value without blocking writes hashmap? i'd know instant value present on it.

myval = state.asyncresponses[myid] 

reading shared map without blocking writers definition of data race. actually, semantically data race even when writers blocked during read! because finish reading value , unblock writers - value may not exists in map anymore.

anyway, it's not proper syncing bottleneck in many programs. non-blocking lock af {rw,}mutex in order of < 20 nsecs on middle powered cpus. suggest postpone optimization not after making program correct, after measuring major part of time being spent.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -