Función atomic.LoadPointer() en Golang con ejemplos

En el lenguaje Go, los paquetes atómicos proporcionan una memoria atómica de nivel inferior que es útil para implementar algoritmos de sincronización. La función LoadPointer() en el lenguaje Go se usa para cargar atómicamente *addr . Esta función se define en el paquete atómico. Aquí, debe importar el paquete «sincronización/atómico» e «inseguro» para poder utilizar estas funciones.
Sintaxis: 
 

func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)

Aquí, addr indica dirección.
Nota: (*unsafe.Pointer) es el puntero a un valor de unsafe.Pointer . Y unsafe.Pointer type es útil para permitir transiciones entre tipos arbitrarios y el tipo uintptr integrado . Además, unsafe es un paquete que es útil en la seguridad de tipo de los programas Go.
Valor devuelto: carga automáticamente *addr y devuelve unsafe.Pointer .
Ejemplo 1:
 

C

// Program to illustrate the usage of
// LoadPointer function in Golang
 
// Including main package
package main
 
// importing fmt,
// sync/atomic and unsafe
import (
    "fmt"
    "sync/atomic"
    "unsafe"
)
 
// Defining a struct type L
type L struct{ x, y, z int }
 
// Declaring pointer
// to L struct type
var PL *L
 
// Calling main
func main() {
 
    // Defining *addr unsafe.Pointer
    var unsafepL = (*unsafe.Pointer)(unsafe.Pointer(&PL))
 
    // Defining value
    // of unsafe.Pointer
    var px L
 
    // Calling StorePointer and
    // storing unsafe.Pointer
    // value to *addr
    atomic.StorePointer(
        unsafepL, unsafe.Pointer(&px))
 
    // Calling LoadPointer() method
    px1 := (*L)(atomic.LoadPointer(unsafepL))
 
    // Returns true if *addr is
    // loaded else returns false
    fmt.Println(px1 == &px)
 
    // Prints unsafe.Pointer
    fmt.Println(&px1)
}

Producción: 
 

true
0xc0000b8018 // Can be different at different run times

Aquí, el método StorePointer agrega valor a *addr y luego el método LoadPointer carga atómicamente *addr. Entonces, aquí se logra la carga, por lo tanto, se devuelve verdadero y el valor de inseguro . El puntero devuelto aquí puede ser diferente en diferentes tiempos de ejecución.
Ejemplo 2:
 

C

// Program to illustrate the usage of
// LoadPointer function in Golang
 
// Including main package
package main
 
// importing fmt,
// sync/atomic and unsafe
import (
    "fmt"
    "sync/atomic"
    "unsafe"
)
 
// Defining a struct type L
type L struct{ x, y, z int }
 
// Declaring pointer
// to L struct type
var PL *L
 
// Calling main
func main() {
 
    // Defining *addr unsafe.Pointer
    var unsafepL = (*unsafe.Pointer)(unsafe.Pointer(&PL))
 
    // Defining value
    // of unsafe.Pointer
    var px L
 
    // Calling LoadPointer() method
    px1 := (*L)(atomic.LoadPointer(unsafepL))
 
    // Returns true if *addr is
    // loaded else returns false
    fmt.Println(px1 == &px)
 
    // Prints unsafe.Pointer
    fmt.Println(&px1)
}

Producción:
 

false
0xc00000e028  // A random value is returned in each run

Aquí, se devuelve false ya que aquí el unsafe.pointer no se almacenó antes, por lo que el método LoadPointer() no pudo cargar el *addr . Además, el valor de dirección devuelto aquí es la dirección de px1 .
 

Publicación traducida automáticamente

Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *