Logo

Programming-Idioms

History of Idiom 33 > diff from v17 to v18

Edit summary for version 18 by :
New Python implementation by user [TinyFawks]

Version 17

2016-02-16, 20:22:05

Version 18

2016-02-18, 16:57:58

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/