Logo

Programming-Idioms

History of Idiom 33 > diff from v15 to v16

Edit summary for version 16 by :
[Go] Comment emphasize

Version 15

2016-01-01, 00:45:38

Version 16

2016-01-01, 01:38:26

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"
Imports
import "sync"
Code
lock := &sync.RWMutex{}

lock.Lock()
x = f(x)
lock.Unlock()
Code
lock := &sync.RWMutex{}

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