Skip to content

Instantly share code, notes, and snippets.

@ErturkOzturk
Created February 2, 2024 23:20
Show Gist options
  • Save ErturkOzturk/c784eabb0666699afeb4e219bcfa111a to your computer and use it in GitHub Desktop.
Save ErturkOzturk/c784eabb0666699afeb4e219bcfa111a to your computer and use it in GitHub Desktop.
DoubleCheckedLocking
public class ClassToLock { }
volatile ClassToLock? _classToLock;
readonly object _classLock = new();
public ClassToLock myClass
{
get
{
if (_classToLock == null) // First check (outside lock)
lock (_classLock)
if (_classToLock == null) // Second check (inside lock)
_classToLock = new ClassToLock();
return _classToLock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment