[#BloombergLP-bsls-SpinLock] = xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bsls.adoc[bsls]::SpinLock :relfileprefix: ../../ :mrdocs: A statically‐initializable synchronization primitive that "spins" (i.e., executes user instructions in a tight loop) rather than blocking waiting threads using system calls. The following idiom is used to initialize `SpinLock` variables: ` SpinLock lock = BSLS_SPINLOCK_UNLOCKED; ` A class member `d_lock` of type `SpinLock` may be initialized using the following idiom: ` , d_lock(SpinLock::s_unlocked) ` == Synopsis Declared in `<bsls_spinlock.h>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- struct SpinLock; ---- == Member Functions [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bsls/SpinLock/2constructor-01.adoc[`SpinLock`] [.small]#[constructor]# | Constructors | xref:BloombergLP/bsls/SpinLock/lock.adoc[`lock`] | Spin (repeat a loop continuously without using the system to pause or reschedule the thread) until this object is unlocked, then atomically acquire the lock. | xref:BloombergLP/bsls/SpinLock/lockWithBackoff.adoc[`lockWithBackoff`] | Repeat a loop continuously, potentially using the system to pause or reschedule the thread, until this object is unlocked, then atomically acquire the lock. The spinning has backoff logic. Note that this method is recommended when system calls are permissible, significant contention is expected, and no better mutual exclusion primitive is available. | xref:BloombergLP/bsls/SpinLock/lockWithoutBackoff.adoc[`lockWithoutBackoff`] | Spin (repeat a loop continuously without using the system to pause or reschedule the thread) until this object is unlocked, then atomically acquire the lock. The spinning does not perform a backoff. | xref:BloombergLP/bsls/SpinLock/tryLock.adoc[`tryLock`] | Attempt to acquire the lock; optionally specify the `numRetries` times to attempt again if this object is already locked. Return 0 on success, and a non‐zero value if the lock was not successfully acquired. The behavior is undefined unless `0 <= numRetries`. | xref:BloombergLP/bsls/SpinLock/unlock.adoc[`unlock`] | Release the lock. The behavior is undefined unless the current thread holds the lock. |=== == Static Data Members [cols="1,4"] |=== | Name| Description | xref:BloombergLP/bsls/SpinLock/s_unlocked.adoc[`s_unlocked`] | Initializer for an unlocked spin lock. |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#