Click here to Skip to main content
11,415,390 members (76,229 online)
Click here to Skip to main content

Spin Lock

, 16 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Efficient synchronised access to shared resources
<!-- (C) 2001 Craig Henderson -->


Spin locks are a very simple and, in the right context, efficient method of synchronising access to shared resources. The overhead of using Critical Sections or Events for resource acquision is relatively large, but in contrast, a spin lock is very quick.


Predictably, the algorithm for a spin lock is very simple. Each thread will instantiate a CSpinLock with a shared lock variable. When a shared resource is required, the threads call lock() and when the resource has been finished with, call unlock().

lock() will cause the thread to loop until the resource is available. The availability of the resource is defined by the value of shared variable. If the value of this variable is zero, then the resource is available, otherwise it is in use by another thread. When the resource is locked, the shared variable holds the value 0x6b636f6c. This will read "lock" if it is cast to a char *


The CSpinLock class implements a spin lock synchronisation object.


explicit CSpinLock(long *plock = 0)
This is the only class constructor. It passes a pointer to a shared variable to hold the lock value. This parameter can be omitted, in which case an internal class lock variable will be used.

Class destructor. Simply asserts that the object that is going out of scope does not hold the lock.

void lock(void) const
Call this method to acquire a lock to the shared resource.

void unlock(void) const
Call this method to release the lock to the shared resource.

bool has_lock(void) const
Returns true/false to signify if the object currently holds the lock


The only decision to be made in using this class is in how the lock variable is used. The easiest use is to omit the ctor parameter and let the class handle this itself. Each resource that is to be shared can be synchronised using a different template parameter value. Each thread can independently instantiate a CSpinLock object and the class implementation will ensure that the resource is synchronised correctly.


Web Site:

Revision History

17 Jun 2002 - Initial Revision


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Craig Henderson
Technical Lead
United Kingdom United Kingdom
Craig graduated with a B.SC. Honours in Computing for Real Time Systems from the University of the West of England, Bristol in 1995 after completing an HND in Computing in 1993.
Follow on   Twitter

Comments and Discussions

GeneralUse Sleep( 0 ) Pin
ajohnson12320-Mar-04 15:20
memberajohnson12320-Mar-04 15:20 
GeneralRe: Use Sleep( 0 ) Pin
bkausbk11-Apr-04 9:48
memberbkausbk11-Apr-04 9:48 
QuestionDid you test the code before posting? Pin
Anonymous17-Jun-02 19:20
memberAnonymous17-Jun-02 19:20 
AnswerRe: Did you test the code before posting? Pin
Anonymous17-Jun-02 19:25
memberAnonymous17-Jun-02 19:25 
AnswerRe: Did you test the code before posting? Pin
Craig Henderson21-Jun-02 1:54
memberCraig Henderson21-Jun-02 1:54 
1. As you say, this is in the documentation

2. I would not consider this a bug with the implementation. Perhaps I should have documented it better, but hey, I can't think of every eventuality Frown | :( A spin lock should only ever be held locked for a very short time, and by implication would not be nested. If the lock is held for any significant amount of time, then other threads would consume high CPU cycles while waiting for the lock, and this would be incredibly inefficient. In such a case, an Event would be more appropriate. Sorry for not being specific about this.

3. Yes, the has_lock() method actually implements an is_locked(), whereby it returns true if the lock is held by _any_ thread. Ooops Big Grin | :-D Getting the class to perform this level of validation is, I think, too much. The spin lock implementation is designed to be rapid, and we are getting way too detailed with this level of functionality. At some point, the programmer must take responsibility for writing solid code, and I think this is true in this case. Again, perhaps the documentation let me down here. Who _likes_ writing documentation?
GeneralBug in has_lock() Pin
Eric Kenslow17-Jun-02 12:25
memberEric Kenslow17-Jun-02 12:25 
GeneralRe: Bug in has_lock() Pin
Craig Henderson21-Jun-02 1:56
memberCraig Henderson21-Jun-02 1:56 
GeneralA small almost irrelevant point Pin
Nish - Native CPian23-May-02 2:40
memberNish - Native CPian23-May-02 2:40 
GeneralRe: A small almost irrelevant point Pin
Craig Henderson23-May-02 3:44
memberCraig Henderson23-May-02 3:44 
GeneralRe: A small almost irrelevant point Pin
Joao Vaz23-May-02 12:19
memberJoao Vaz23-May-02 12:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.4 | Last Updated 17 Jun 2002
Article Copyright 2002 by Craig Henderson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid