Logo

Programming-Idioms

History of Idiom 33 > diff from v14 to v15

Edit summary for version 15 by :
New Go implementation by user [lck]

Version 14

2015-11-30, 12:37:28

Version 15

2016-01-01, 00:45:38

Idiom #33 Atomically read and update variable

Assign variable x the new value f(x), making sure that no other thread may modify x between the read and the write.

Idiom #33 Atomically read and update variable

Assign variable x the new value f(x), making sure that no other thread may modify x between the read and the write.

Imports
import "sync"
Code
lock := &sync.RWMutex{}

lock.Lock()
x = f(x)
lock.Unlock()
Comments bubble
You need to lock whenever accessing x
Doc URL
https://golang.org/pkg/sync/