在Go語言中,競態(tài)條件(race condition)是指多個(gè)并發(fā)執(zhí)行的goroutine訪問共享資源時(shí),由于操作順序不當(dāng)導(dǎo)致的不確定結(jié)果。為了避免競態(tài)條件,你可以采用以下方法:
sync.Mutex
結(jié)構(gòu)體來實(shí)現(xiàn)互斥鎖。import (
"fmt"
"sync"
)
var (
counter = 0
mutex sync.Mutex
)
func increment() {
mutex.Lock()
counter++
mutex.Unlock()
}
func main() {
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
increment()
}()
}
wg.Wait()
fmt.Println("Counter:", counter)
}
import (
"fmt"
"sync"
)
var (
data = []int{0}
rwMutex sync.RWMutex
)
func read() int {
rwMutex.RLock()
defer rwMutex.RUnlock()
return data[0]
}
func write(value int) {
rwMutex.Lock()
defer rwMutex.Unlock()
data[0] = value
}
func main() {
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
read()
}()
}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(value int) {
defer wg.Done()
write(value)
}(i)
}
wg.Wait()
fmt.Println("Data:", data[0])
}
import (
"fmt"
"sync"
)
func increment(counter chan int, wg *sync.WaitGroup) {
defer wg.Done()
counter <- 1
}
func main() {
var counter int
var wg sync.WaitGroup
counterChan := make(chan int, 1)
wg.Add(10)
for i := 0; i < 10; i++ {
go increment(counterChan, &wg)
}
go func() {
wg.Wait()
close(counterChan)
}()
for value := range counterChan {
counter += value
}
fmt.Println("Counter:", counter)
}
sync/atomic
包提供了一些原子操作函數(shù),如AddInt32
、CompareAndSwapInt32
等。import (
"fmt"
"sync/atomic"
)
var counter int32
func increment() {
atomic.AddInt32(&counter, 1)
}
func main() {
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
increment()
}()
}
wg.Wait()
fmt.Println("Counter:", counter)
}
總之,要避免競態(tài)條件,你需要確保在同一時(shí)間只有一個(gè)goroutine訪問共享資源。你可以使用互斥鎖、讀寫鎖、通道或原子操作來實(shí)現(xiàn)這一目標(biāo)。在實(shí)際編程中,你需要根據(jù)具體場景選擇合適的同步機(jī)制。