I am not sure why the following code has the race condition, can someone give me a hint? I think there is no potential race condition. Thank you in advance.
type PossiblySafeCounter struct {
mu sync.Mutex
sum int
}
func (c *PossiblySafeCounter) inc() {
c.mu.Lock();
defer c.mu.Unlock();
go func() {
c.sum++
}()
}
func (c *PossiblySafeCounter) read() int {
c.mu.Lock();
defer c.mu.Unlock();
return c.sum
}
The c.sum++
is in a goroutine that is scheduled independently of the execution of the inc()
method. When the inc()
method exits the defer
ed unlock of the mutex will happen and will very likely happen at the wrong time, leading to a race condition.
As @Flimzy suggests using atomic.AddInt32
would remove the need for a mutex at all.
two mutex based solutions are either to not increment in a goroutine:
func (c *PossiblySafeCounter) inc() {
c.mu.Lock();
defer c.mu.Unlock();
c.sum++
}
or do the locking and unlocking in the goroutine:
func (c *PossiblySafeCounter) inc() {
go func() {
c.mu.Lock();
defer c.mu.Unlock();
c.sum++
}()
}
but honestly, doing any kind of goroutine doesn't make sense in this example. Why do you need the increment to be in a goroutine?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.