Created
February 2, 2024 23:20
-
-
Save ErturkOzturk/c784eabb0666699afeb4e219bcfa111a to your computer and use it in GitHub Desktop.
DoubleCheckedLocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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