- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Atomic Variable in Golang
Atomic Variable in Golang offer an alternative to use locks or other synchronisation primitives to execute atomic operations on shared variables. When programming concurrently, synchronisation and mutual exclusion are essential to ensuring that threads or processes can access shared resources without interfering with one another. The fast and scalable synchronisation and coordination of concurrent access to shared variables is made possible by the use of atomic variables.
What is an Atomic Variable?
An atomic variable is a shared variable that may be read from and written to simultaneously by several goroutines while still ensuring that all actions are atomic. The operations on an atomic variable are therefore indivisible and cannot be stopped by other threads or processes.
The ‘sync/atomic’ package in the Go programming language offers a variety of atomic variables, including pointers, booleans, and integers. These types support operations like addition, subtraction, compare-and-swap, and load-and-store that are used in concurrent systems to implement synchronisation and mutual exclusion.
How to Use Atomic Variables in Go
You must first import the ‘sync/atomic’ package in Go before you can use atomic variables. Then, you can make an atomic variable by employing one of the types listed below −
‘int32’, ‘int64’ − atomic integer types
‘uint32’, ‘uint64’ − atomic unsigned integer types
‘uintptr’ − atomic pointer type
‘bool’ − atomic boolean type
Once an atomic variable has been created, atomic operations can be carried out on it using the functions offered by the'sync/atomic' package.
Example
package main import ( "fmt" "sync/atomic" ) func main() { var counter int64 = 0 // Atomic increment atomic.AddInt64(&counter, 1) // Atomic compare-and-swap expected := int64(0) new := int64(1) atomic.CompareAndSwapInt64(&counter, expected, new) // Atomic load value := atomic.LoadInt64(&counter) fmt.Println(value) }
Output
1
Here, we construct the type ‘int64’ atomic variable ‘counter’ and initialise it to zero. The variable is then subjected to three atomic operations: load, compare-and-swap, and increment.
The ‘AddInt64’ function atomically increases the variable by the specified amount. The "CompareAndSwapInt64" function executes an atomic compare-and-swap operation on the variable, changing its value only if its current value matches the predicted value. The 'LoadInt64' function then loads the variable's current value atomically.
Conclusion
Without the use of locks or other synchronisation primitives, atomic operations on shared variables can be carried out quickly and effectively in Go thanks to atomic variables. While still preserving great efficiency and scalability, you can use atomic variables to make sure that concurrent access to shared resources is synchronised and mutually exclusive.