The lock
statement ensures that one thread can hold the object being locked at at a given time and other threads attempting to lock that object are blocked until the lock is released - usually when the block is exited.
With C# 13 a new System.Threading.Lock object is introduced that can be used with the lock
statement and using
directive. The Lock
object is a disposable object that can be used to lock a block of code and automatically release the lock when the block is exited.
Notes
- Using
lock
with a newLock
object will use it's methods to provide locking and unlocking - Other objects continue to be wrapped by System.Thread.Monitor