Skip to content

Instantly share code, notes, and snippets.

@rofr
Created August 13, 2020 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rofr/96a065faaff3383e67a58b96d7adf1fb to your computer and use it in GitHub Desktop.
Save rofr/96a065faaff3383e67a58b96d7adf1fb to your computer and use it in GitHub Desktop.
Is object assignment in .NET atomic?
class AtomicAssignment
{
static object o;
static AtomicAssignment() => o = new object();
public static Update()
{
//is the assignment atomic? or is something like Interlocked.Exchange necessary?
o = new object();
}
public static Read()
{
//if the assignment in Update() is atomic then this should be thread safe
//we will get a valid reference to an object
object refCopy = o;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment