Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C++
Article

Spin Lock

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
16 Jun 20022 min read 167.9K   742   12   51
Efficient synchronised access to shared resources

Introduction

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.

Algorithm

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 *

Implementation

The CSpinLock class implements a spin lock synchronisation object.

Methods

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.

~CSpinLock()
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

Usage

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.

Contact

Web Site: http://homepage.virgin.net/cdm.henderson
e-mail: cdm.henderson@virgin.net

Revision History

17 Jun 2002 - Initial Revision

License

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


Written By
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.

Comments and Discussions

 
GeneralUse Sleep( 0 ) Pin
ajohnson12320-Mar-04 14:20
ajohnson12320-Mar-04 14:20 
GeneralRe: Use Sleep( 0 ) Pin
bkausbk11-Apr-04 8:48
bkausbk11-Apr-04 8:48 
QuestionDid you test the code before posting? Pin
17-Jun-02 18:20
suss17-Jun-02 18:20 
The reason I am asking this is that I found the following 3 problems.

1. First, the constructor. The parameter is just a pointer to a lock variable. You did not say that the variable has to be initialized to 0. But if it is not initialized to 0, then every thread calling the lock method will go into an infinite loop. Ok, maybe you just missed this.

2. Secondly, the lock method cannot be called by the same thread again, otherwise it goes into an infinite loop. This may be a defect instead of a bug. The problem is you did not say it in the article (that the lock method cannot be called twice by the same thread).

3. Finally, if a thread called the lock method successfully, a different thread can unlock the same resource (because your has_lock method is buggy, as pointed out in another comment).

Sorry for sounding too hush, I just had a bad day. Poke tongue | ;-P

AnswerRe: Did you test the code before posting? Pin
17-Jun-02 18:25
suss17-Jun-02 18:25 
AnswerRe: Did you test the code before posting? Pin
Craig Henderson21-Jun-02 0:54
Craig Henderson21-Jun-02 0:54 
GeneralBug in has_lock() Pin
Eric Kenslow17-Jun-02 11:25
Eric Kenslow17-Jun-02 11:25 
GeneralRe: Bug in has_lock() Pin
Craig Henderson21-Jun-02 0:56
Craig Henderson21-Jun-02 0:56 
GeneralA small almost irrelevant point Pin
Nish Nishant23-May-02 1:40
sitebuilderNish Nishant23-May-02 1:40 
GeneralRe: A small almost irrelevant point Pin
Craig Henderson23-May-02 2:44
Craig Henderson23-May-02 2:44 
GeneralRe: A small almost irrelevant point Pin
Joao Vaz23-May-02 11:19
Joao Vaz23-May-02 11:19 
GeneralRe: A small almost irrelevant point Pin
Nish Nishant23-May-02 15:39
sitebuilderNish Nishant23-May-02 15:39 
GeneralAnother alternative for Spin Lock Pin
Maxime Labelle23-May-02 1:28
Maxime Labelle23-May-02 1:28 
GeneralNot a bad attempt but ... Pin
Joao Vaz23-May-02 0:52
Joao Vaz23-May-02 0:52 
GeneralRe: Not a bad attempt but ... Pin
Wes Jones23-May-02 8:06
Wes Jones23-May-02 8:06 
GeneralRe: Not a bad attempt but ... Pin
Matt Gullett23-May-02 8:31
Matt Gullett23-May-02 8:31 
GeneralRe: Not a bad attempt but ... Pin
Joao Vaz23-May-02 11:09
Joao Vaz23-May-02 11:09 
GeneralCritical sections etc. + Metered Sections Pin
Neville Franks23-May-02 12:38
Neville Franks23-May-02 12:38 
GeneralRe: Critical sections etc. + Metered Sections Pin
Matt Gullett23-May-02 12:43
Matt Gullett23-May-02 12:43 
GeneralRe: Critical sections etc. + Metered Sections Pin
Neville Franks23-May-02 12:53
Neville Franks23-May-02 12:53 
GeneralRe: Critical sections etc. + Metered Sections Pin
Matt Gullett23-May-02 13:04
Matt Gullett23-May-02 13:04 
GeneralRe: Critical sections etc. + Metered Sections Pin
Joao Vaz23-May-02 19:57
Joao Vaz23-May-02 19:57 
GeneralRe: Not a bad attempt but ... Pin
Tim Smith23-May-02 8:26
Tim Smith23-May-02 8:26 
GeneralRe: Not a bad attempt but ... Pin
Joao Vaz23-May-02 10:44
Joao Vaz23-May-02 10:44 
GeneralRe: Not a bad attempt but ... Pin
Joao Vaz23-May-02 11:29
Joao Vaz23-May-02 11:29 
GeneralRe: Not a bad attempt but ... Pin
Tim Smith23-May-02 12:59
Tim Smith23-May-02 12:59 

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

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