The SpinLock structure in .Net can be used to manage access to resources from multiple threads. Other than a normal lock it uses a busy waiting, which is faster if the expected wait time is very low (but consumes more resources).
Other threading primitives such as a Monitor
and lock(...){}
always acquire the lock (or wait forever to acquire it). But the SpinLock.Enter
method uses a ref bool
parameters to indicate wether or not acquiring the lock failed.
What is the ref bool lockTaken
needed and in what cases can Monitor.Enter
fail (and thus set lockTaken
to false?)