Skip to content

Instantly share code, notes, and snippets.

@deepumi
Last active May 14, 2019 12:58
Show Gist options
  • Select an option

  • Save deepumi/b44ee1b356adbc2d00174a973dadff8a to your computer and use it in GitHub Desktop.

Select an option

Save deepumi/b44ee1b356adbc2d00174a973dadff8a to your computer and use it in GitHub Desktop.
Thready safety in Static Variables
//Interlocked class Provides atomic operations for variables that are shared by multiple threads.
private static string _data;
public ActionResult Index()
{
//Compares two instances of the specified reference type <paramref name="T" /> for equality and, if they are equal, replaces the first one
if (Interlocked.CompareExchange(ref _data, "test", null) == null)
{
}
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment