在Go語言中,原子包提供lower-level原子內存,這對實現同步算法很有幫助。 Go語言中的AddUintptr()函數用於自動將增量添加到* addr。此函數在原子包下定義。在這裏,您需要導入“sync/atomic”軟件包才能使用這些函數。
用法:
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
在此,addr表示地址,而delta表示少量大於零的位。
注意:(* uintptr)是指向uintptr值的指針。 uintptr是一個足夠大的整數類型,可以容納任何指針的位模式。
返回值:它自動添加addr和delta並返回一個新值。
範例1:
// Golang Program to illustrate the usage of
// AddUintptr function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Main function
func main() {
// Assigning variable
// values to the uintptr
var (
w uintptr = 0
x uintptr = 255
y uintptr = 564688
z uintptr = 656757686877
)
// Assigning constant
// values to uintptr
const (
m uintptr = 78
n uintptr = 96
)
// Calling AddUintptr method
// with its parameters
p_1:= atomic.AddUintptr(&x, (m))
p_2:= atomic.AddUintptr(&y, ^(n - 1))
p_3:= atomic.AddUintptr(&z, (2))
p_4:= atomic.AddUintptr(&w, (n - m))
// Displays the output after adding
// addr and delta automically
fmt.Println(p_1)
fmt.Println(p_2)
fmt.Println(p_3)
fmt.Println(p_4)
}
輸出:
333 564592 656757686879 18
範例2:
// Golang Program to illustrate the usage of
// AddUintptr function
// Including main package
package main
// importing fmt and sync/atomic
import (
"fmt"
"sync/atomic"
)
// Defining addr of type uintptr
type addr uintptr
// function that adds addr and delta
func (p *addr) adds() uintptr {
// Calling AddUintptr()
// function with its
// parameter
return atomic.AddUintptr((*uintptr)(p), 32686776785)
}
// Main function
func main() {
// Defining p
var p addr
// For loop to increment
// the value of p
for i:= 4; i < 1000; i *= 5 {
// Displays the new value after
// adding delta and addr
fmt.Println(p.adds())
}
}
輸出:
32686776785 65373553570 98060330355 130747107140
在上麵的示例中,我們定義了一個add函數,該函數返回調用AddUintptr方法返回的輸出。在主函數中,我們定義了一個“for”循環,該循環將在每個調用中增加‘p’的值。在此,AddUintptr()方法的第二個參數是恒定的,隻有第一個參數的值是可變的。但是,上一個調用的輸出將是下一個調用中AddUintptr()方法的第一個參數的值,直到循環停止。
讓我們看看上麵的示例如何工作:
1st parameter = 0, 2nd parameter = 32686776785 // returns (0 + 32686776785 = 32686776785) // Now, the above output is 1st parameter // in next call to AddUintptr() method // It returns (32686776785 + 32686776785 = 65373553570) 1st parameter = 32686776785, 2nd parameter = 32686776785 // returns (65373553570 + 32686776785 = 130747107140) and so on 1st parameter = 65373553570, 2nd parameter = 32686776785
相關用法
- 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.AddUintptr() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。