Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Go support volatile / non-volatile variables?

Tags:

I'm new to the language so bear with me.

I am curious how GO handles data storage available to threads, in the sense that non-local variables can also be non-volatile, like in Java for instance.

GO has the concept of channel, which, by it's nature -- inter thread communication, means it bypasses processor cache, and reads/writes to heap directly.

Also, have not found any reference to volatile in the go lang documentation.

like image 885
Raul Avatar asked Aug 28 '13 14:08

Raul


People also ask

What happens when a variable is declared volatile?

Volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time-without any action being taken by the code the compiler finds nearby.

Which variables are volatile?

A volatile variable is a variable that is marked or cast with the keyword "volatile" so that it is established that the variable can be changed by some outside factor, such as the operating system or other software.

When should volatile be used?

Yes, volatile must be used whenever you want a mutable variable to be accessed by multiple threads. It is not very common usecase because typically you need to perform more than a single atomic operation (e.g. check the variable state before modifying it), in which case you would use a synchronized block instead.

Where are volatile variables used?

Volatile is used in C programming when we need to go and read the value stored by the pointer at the address pointed by the pointer. If you need to change anything in your code that is out of compiler reach you can use this volatile keyword before the variable for which you want to change the value.


1 Answers

TL;DR: Go does not have a keyword to make a variable safe for multiple goroutines to write/read it. Use the sync/atomic package for that. Or better yet Do not communicate by sharing memory; instead, share memory by communicating.


Two answers for the two meanings of volatile volatile Ven diagram

.NET/Java concurrency

Some excerpts from the Go Memory Model.

If the effects of a goroutine must be observed by another goroutine, use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

One of the examples from the Incorrect Synchronization section is an example of busy waiting on value.

Worse, there is no guarantee that the write to done will ever be observed by main, since there are no synchronization events between the two threads. The loop in main is not guaranteed to finish.

Indeed, this code(play.golang.org/p/K8ndH7DUzq) never exits.

C/C++ non-standard memory

Go's memory model does not provide a way to address non-standard memory. If you have raw access to a device's I/O bus you'll need to use assembly or C to safely write values to the memory locations. I have only ever needed to do this in a device driver which generally precludes use of Go.

like image 102
deft_code Avatar answered Nov 09 '22 05:11

deft_code