Skip to content

Commit fbfc8f3

Browse files
Fixed the mutex to make it more readable.
1 parent be435ff commit fbfc8f3

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

chapter6/example07/example07.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ func incCounter(id int) {
4444
// Only allow one goroutine through this
4545
// critical section at a time.
4646
mutex.Lock()
47+
{
48+
// Capture the value of counter.
49+
value := counter
4750

48-
// Capture the value of counter.
49-
value := counter
51+
// Yield the thread and be placed back in queue.
52+
runtime.Gosched()
5053

51-
// Yield the thread and be placed back in queue.
52-
runtime.Gosched()
53-
54-
// Increment our local value of counter.
55-
value++
56-
57-
// Store the value back into counter.
58-
counter = value
54+
// Increment our local value of counter.
55+
value++
5956

57+
// Store the value back into counter.
58+
counter = value
59+
}
60+
mutex.Unlock()
6061
// Release the lock and allow any
6162
// waiting goroutine through.
62-
mutex.Unlock()
6363
}
6464
}

0 commit comments

Comments
 (0)