LockFreeMap

Run Settings
LanguageGo
Language Version
Run Command
package main import ( "sync/atomic" "unsafe" ) func newLockFreeCache() lockFreeCache { ptr := unsafe.Pointer(new(map[string]string)) cache := lockFreeCache{ ptr: &ptr, } return cache } type lockFreeCache struct { ptr *unsafe.Pointer } func (cache lockFreeCache) read(key string) (string, bool) { v := (*map[string]string)(atomic.LoadPointer(cache.ptr)) value, ok := (*v)[key] return value, ok } func (cache lockFreeCache) write(key, value string) { v := (*map[string]string)(atomic.LoadPointer(cache.ptr)) nm := make(map[string]string) for k, v := range *v { nm[k] = v } nm[key] = value nmptr := unsafe.Pointer(&nm) atomic.StorePointer(cache.ptr, nmptr) } func main() { cache := newLockFreeCache() cache.write("key", "YEAH~!") val, _ := cache.read("key") println(val) }
Editor Settings
Theme
Key bindings
Full width
Lines