在Go語言中,原子包提供lower-level原子內存,這對實現同步算法很有幫助。 Go語言中的AddUint32()函數用於將增量自動添加到* addr中。此函數在原子包下定義。在這裏,您需要導入“sync/atomic”軟件包才能使用這些函數。
用法:
func AddUint32(addr *uint32, delta uint32) (new uint32)
在此,addr表示地址,而delta表示少量大於零的位。此外,如果要從x減去帶符號的正常數值c,則可以通過AddUint32(&x ;,^uint32(c-1))來實現。而且,如果要特別減小x,則可以通過AddUint32(&x ;,^uint32(0))完成。
注意:(* uint32)是指向uint32值的指針。 uint32是位大小為32的無符號整數類型。此外,uint32包含從0到4294967295的所有無符號32位整數的集合。
返回值:它自動添加addr和delta並返回一個新值。
範例1:
// Golang Program to illustrate the usage
// of AddUint32 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Assigning values to the uint32
var (
i uint32 = 45
j uint32 = 67
k uint32 = 4294967295
l uint32 = 0
z int = 5
)
// Assigning constant
// values to uint32
const (
x uint32 = 6
y uint32 = 3
)
// Calling AddUint32 method
// with its parameters
a_1:= atomic.AddUint32(&i, -uint32(z))
a_2:= atomic.AddUint32(&j, ^(y - 1))
a_3:= atomic.AddUint32(&k, x-1)
a_4:= atomic.AddUint32(&l, ^uint32(z-1))
// Displays the output after adding
// addr and delta automically
fmt.Println(a_1)
fmt.Println(a_2)
fmt.Println(a_3)
fmt.Println(a_4)
}
輸出:
40 64 4 4294967291
範例2:
// Golang program to illustrate the usage
// of the AddUint32 function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Defining addr of type uint32
type addr uint32
// function that adds addr and delta
func (u *addr) add() uint32 {
// Calling AddUint32() function
// with its parameter
return atomic.AddUint32((*uint32)(u), 4)
}
// Main function
func main() {
// Defining u
var u addr
// For loop to increment the value of u
for i:= 0; i < 17; i += 2 {
// Displays the new value
// after adding delta and addr
fmt.Println(u.add())
}
}
輸出:
4 8 12 16 20 24 28 32 36
在上麵的示例中,我們定義了一個函數add,該函數返回從調用AddUint32方法返回的輸出。在主函數中,我們定義了一個“for”循環,該循環將在每個調用中增加‘u’的值。在這裏,AddUint32()方法的第二個參數是恒定的,隻有第一個參數的值是可變的。但是,上一個調用的輸出將是下一個調用中AddUint32()方法的第一個參數的值,直到循環停止。
讓我們看看上麵的示例如何工作:
1st parameter = 0, 2nd parameter = 4 // returns (0 + 4 = 4) // Now, the above output is 1st parameter in next call to AddUint32() method 1st parameter = 4, 2nd parameter = 4 // returns (4 + 4 = 8) 1st parameter = 8, 2nd parameter = 4 // returns (8 + 4 = 12) and so on.
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 atomic.AddUint32() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。