Created
December 10, 2012 22:19
-
-
Save igm/4253873 to your computer and use it in GitHub Desktop.
Lock based map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "sync" | |
| ) | |
| type conn struct{} | |
| type connectionMap struct { | |
| connections map[string]*conn | |
| lock sync.Mutex | |
| } | |
| func NewMap() *connectionMap { | |
| return &connectionMap{ | |
| connections: make(map[string]*conn), | |
| } | |
| } | |
| func (cm *connectionMap) get(key string) *conn { | |
| cm.lock.Lock() | |
| defer cm.lock.Unlock() | |
| return cm.connections[key] | |
| } | |
| func (cm *connectionMap) set(key string, c *conn) { | |
| cm.lock.Lock() | |
| defer cm.lock.Unlock() | |
| cm.connections[key] = c | |
| } | |
| func main() { | |
| myMap := NewMap() | |
| myMap.set("key", new(conn)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment