Исходники и презентации
This commit is contained in:
15
lessons/sync_primitives/cas_loop/main.go
Normal file
15
lessons/sync_primitives/cas_loop/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func IncrementAndGet(pointer *int32) int32 {
|
||||
for {
|
||||
currentValue := atomic.LoadInt32(pointer)
|
||||
nextValue := currentValue + 1
|
||||
if atomic.CompareAndSwapInt32(pointer, currentValue, nextValue) {
|
||||
return nextValue
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user