This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
TheSpinLock structure is a low-level, mutual-exclusion synchronization primitive that spins while it waits to acquire a lock. On multicore computers, when wait times are expected to be short and when contention is minimal,SpinLock can perform better than other kinds of locks. However, we recommend that you useSpinLock only when you determine by profiling that theSystem.Threading.Monitor method or theInterlocked methods are significantly slowing the performance of your program.
SpinLock may yield the time slice of the thread even if it has not yet acquired the lock. It does this to avoid thread-priority inversion, and to enable the garbage collector to make progress. When you use aSpinLock, ensure that no thread can hold the lock for more than a very brief time span, and that no thread can block while it holds the lock.
Because SpinLock is a value type, you must explicitly pass it by reference if you intend the two copies to refer to the same lock.
For more information about how to use this type, seeSystem.Threading.SpinLock. For an example, seeHow to: Use SpinLock for Low-Level Synchronization.
SpinLock supports athread-tracking mode that you can use during the development phase to help track the thread that is holding the lock at a specific time. Thread-tracking mode is very useful for debugging, but we recommend that you turn it off in the release version of your program because it may slow performance. For more information, seeHow to: Enable Thread-Tracking Mode in SpinLock.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?