Sunday, August 23, 2020

Volatile Variables in Golang

   

In the past, I have used lockless code when there is a single reader and single writer and the reader can accept stale information to some extent (for example, this sample code on my github) .



However, there is a problem in this code. If the two go routines run on different cpu cores, then the reader go routines may never get changes from writer go routine? So, the solution I typically employed in languages such as C/C++ is to use volatile type for flag. However, go doesn't seem to support volatile variables as per these discussions: google groups discussion, stackoverflow discussion!! 

So, the solution for the above situation is to use an atomic variable. To find the correct way of writing the above code, please check here.


No comments:

UA-48797665-1