Q:

Golang program to add value to a uintprt variable in an uninterruptible manner

belongs to collection: Golang sync/atomic Package Programs

0

Here, we will add a number with the uintptr variable in a synchronized manner. Here, we will use atomic.AddUintptr() function. The atomic.AddUintptr() function is used in synchronized manner for multithreaded/multi-process environment.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to add value to a 'uintprt' variable in an uninterruptible manner is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Golang program to add value to a 'uintprt' variable
// in an uninterruptible manner

package main

import "fmt"
import "sync/atomic"

// Entry point of the program
func main() {
	var num1 uintptr = 235
	var num2 uintptr = 250

	res1 := atomic.AddUintptr(&num1, 50)
	res2 := atomic.AddUintptr(&num2, 70)

	fmt.Println("Result1: ", res1)
	fmt.Println("Result2: ", res2)
}

Output:

Result1:  285
Result2:  320

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the required packages to predefined functions.

In the main() function, we created a uintptr variable. Then we added the values to the variables using atomic.AddUintptr() function one by one. After that, we printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang program to demonstrate the atomic.LoadInt32... >>
<< Golang program to add value to a 64-bit unsigned i...