lock statement in LINQPad and async code
The LINQPad
and LINQPad.Controls
classes rely on lock
statements. How does it coexist with async
code?
// LINQPad.DumpContainer public string Style { get { lock (_lock) { return _style; } } set { lock (_lock) { if (!(_style == value)) { _style = value; this.StyleChanged(this, EventArgs.Empty); } } } }
Comments
I don't understand the question. The property implementation that you provided is synchronous, and the lock protects the field from mutlithreaded access.
The question is how it would work with
async
code, becauselock
has thread affinity.Are you referring to the restriction whereby you can't await inside a lock? This doesn't apply here because no awaiting is taking place. The same thread that enters the lock also releases the lock.
There's nothing that prevents asynchronous code from calling synchronous code that obtains and releases locks on the same thread.