Click here to Skip to main content
15,891,905 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 168.1K   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

 
GeneralA good MSDN article Pin
22-May-02 20:33
suss22-May-02 20:33 
GeneralRe: A good MSDN article Pin
Craig Henderson22-May-02 22:13
Craig Henderson22-May-02 22:13 
GeneralRe: A good MSDN article Pin
Dudi Avramov22-May-02 22:22
Dudi Avramov22-May-02 22:22 
GeneralRe: A good MSDN article Pin
Wes Jones23-May-02 8:55
Wes Jones23-May-02 8:55 
GeneralRe: A good MSDN article Pin
Dudi Avramov26-May-02 0:17
Dudi Avramov26-May-02 0:17 
GeneralRe: A good MSDN article Pin
Neville Franks23-May-02 12:21
Neville Franks23-May-02 12:21 
GeneralAnother alternative for SwitchToThread Pin
22-May-02 20:26
suss22-May-02 20:26 
GeneralRe: Another alternative for SwitchToThread Pin
23-May-02 8:04
suss23-May-02 8:04 
I do not think this is accurate. At least not on NT/2000. I previously worked for an H.323 toolkit vendor, and as I recall, we found that a call to

Sleep(0);

actually did not cause the thread to give up the remainder of its timeslice, but was apparently ignored by the OS. However, a call to

Sleep(1);

most certainly cause the desired effect.

We came across this while working on the Solaris version of the code base. We found that on Solaris, we could effect a desired context switch by calling Sleep(0); but that on NT the desired context switch could not be generated unles we passed Sleep() something other than 0.

Jack Knife
GeneralRe: Another alternative for SwitchToThread Pin
Joao Vaz23-May-02 11:22
Joao Vaz23-May-02 11:22 

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.