diff options
author | David Symonds <dsymonds@golang.org> | 2009-04-20 00:42:08 -0700 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2009-04-20 00:42:08 -0700 |
commit | 796accfa71507b7fe90e4ebc35f189c6fb413ae6 (patch) | |
tree | 57895547d034e5093e2bd24183b6c931f12fbe6a /src/lib/exvar.go | |
parent | 476cc3254015d18836029b9e871fb690d27e2ac0 (diff) | |
download | golang-796accfa71507b7fe90e4ebc35f189c6fb413ae6.tar.gz |
Use the mutex in exvar.Set since map access is not atomic.
Imagine your var has a value of zero. If you have a goroutine calling Set(5),
and another calling Increment(+1), then you only want one of these outcomes:
- Set completes first, and then Increment occurs => 6
- Increment completes first, and then Set occurs => 5
However, you could get a sequence:
- read (for Increment) 0
- set (for Set) 5
- write (for Increment) 1
This results in a value of 1, which is undesirable.
Kudos to dnadasi for catching this.
R=r
APPROVED=r
DELTA=3 (3 added, 0 deleted, 0 changed)
OCL=27625
CL=27625
Diffstat (limited to 'src/lib/exvar.go')
-rw-r--r-- | src/lib/exvar.go | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/lib/exvar.go b/src/lib/exvar.go index ccfd34acd..319a0977a 100644 --- a/src/lib/exvar.go +++ b/src/lib/exvar.go @@ -36,6 +36,9 @@ func Increment(name string, inc int) { // Set sets the var called name to value. func Set(name string, value int) { + mutex.Lock(); + defer mutex.Unlock(); + intVars[name] = value } |