ADVERTISEMENT
ADVERTISEMENT

Golang program to add value to a 32-bit unsigned integer in an uninterruptible manner

Here, we are going to learn how to add value to a 32-bit unsigned integer in an uninterruptible manner in Golang (Go Language)?
Submitted by Nidhi, on May 03, 2021

Problem Solution:

Here, we will perform the addition of a number with a 32-bit unsigned integer in a synchronized manner. Here we will use atomic.AddUint32() function. The atomic.AddUint32() function is used in synchronized manner for multithreaded/multi-process environment.

Program/Source Code:

The source code to add value to a 32-bit unsigned integer 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 32-bit unsigned integer
// in an uninterruptible manner

package main

import "fmt"
import "sync/atomic"

// Entry point of the program
func main() {
	var num1 uint32 = 67656
	var num2 uint32 = 67620

	res1 := atomic.AddUint32(&num1, 20)
	res2 := atomic.AddUint32(&num2, 30)

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

Output:

Result1:  67676
Result2:  67650

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 two 32-bit unsigned integer variables. Then we added the values to the variables using the atomic.AddUint32() function one by one. After that, we printed the result on the console screen.

Golang sync/atomic Package Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.