Click here to Skip to main content
Click here to Skip to main content

Hardwired's Thread Safe Single Link List Template

, 26 Feb 2005
Rate this:
Please Sign up or sign in to vote.
This article explains how to use CLinkedListSingle template class. This template is a thread safe implementation of single link list.

Introduction

Most of you know how to implement a Single Linked List. Not so many know how to make a template out of it. Even less know how to make it thread safe. Of course, not all application need Thread Safe Single Link List. Application like notepad, calculator or ping. Hmmm... still...

Anyway, lists and thread safe, are two things that go hand in hand when developing... any kind of application. By making any list thread safe you avoid a lot of problems. Problems which are not easy to trace using a debugger.

...and if all this comes as a template... even better.

All this being said, let's get to the point.

Using the code

First add everything that's in the pack to your project: LinkedListSingle.h, LinkedListSingle.cpp, NamedCriticalSection.h, NamedCriticalSection.cpp, SpinLock.h, SpinLock.cpp, DebugTrace.h and DebugTrace.cpp.

Second, include this where you need to declare a new list:

#include "LinkedListSingle.h"
#include "LinkedListSingle.cpp"    // you need this too, or the code for the list will not be generated!!!

Once you did all this, declare a new list, like this:

//
// somewhere in your application:
//

CLinkedListSingle<int, sinzeof(int)>    IntegerList;

The template can be used with anything... so, let's complicate the things a bit. Let's say we have this class:

//
// some class
//

class SomeClass
{
    double        DVar;    // a double private data member

public:

    char        szString[ 32 ];    // a string public data member

    SomeClass(): DVar( 0 )    { memset( szString, 0, 32 ); };
    ~SomeClass(){};

    void    Increase( void ){ DVar++; };    
}

Now, you want a list of SomeClass elements. You declare it like this:

//
// somewhere in your application:
//

CLinkedListSingle<SomeClass, sinzeof(SomeClass)>    SomeClassList;

CLinkedListSingle

It's not a complex class... once you get to know itSmile | :) .

template <class T, int i> 
class CLinkedListSingle
{
    CDebugTrace    Trace;        // used for debugging

    USHORT        Size;        // size of list

public:
    CLinkedListSingleNode< T, i >    *pHead;        // head of list
    CNamedCriticalSection        Section;    // critical section object used to lock the list
                            // this way using the list in a thread safe manner

    CLinkedListSingle();
    ~CLinkedListSingle();

    bool Initialize();        // initialize the list
    bool Free();            // release all elements of the list

    bool Add(T&);            // add new element to the head of list
    bool Remove(T&);        // remove element - by content
    bool Remove(USHORT);        // remove element - by position
    bool Insert(USHORT, T&);    // insert element in list

    USHORT GetSize(void);        // get the size of list
};

Normally this should not interest you. This class is made to just "set it and use it".

One more thing. If you ever need to directly access the elements of the list - let's say you need to "walk" through all the elements and do something - remember to do this:

//
// somewhere in your application:
//

CLinkedListSingle<SomeClass, sinzeof(SomeClass)>    SomeClassList;
//
// somewhere else in your application (different thread for example)
//

CSpinLock    Spin( &(SomeClass.Section), "NoWhere" );
if( Spin.Lock() )
{
    //
    // direct access over list and elements data
    //
    
    ...

    Spin.Unlock();
}

Points of Interest

Anything involving linked lists. This article refers only to Single Link List, but should not be hard for you to create a Double Link List out of it. I hope soon I will be able to post a new article about it.

History

version 1.0 - 27-february-2005 - So, now it's history!Smile | :)

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

Share

About the Author

Inocentric
Web Developer
Romania Romania
Programming C\C++ since 1995. Live to code another day!Smile | :) . Basicly I code anything in C/C++ with API, Platform SDK, Windows DDK, ODBC, OpenGL SDK, DirectX SDK, etc.
 
For the last six years(starting with 1999) I worked as freelancer.
In 2003 I have co-founded ICode&Ideas SRL, Romania, focusing on security software solutions (Project Manager and Lead Developer of Prisma Firewall).
Starting with 2005 I am the founder and president of Inocentric SRL, Romania.
Group type: Organisation (No members)



Comments and Discussions

 
GeneralMy vote of 1 PinmemberBen_Desjardins12-Oct-10 8:54 
QuestionHow to "walk" through all the elements? Pinmembertermal19-Sep-07 0:25 
GeneralLet me show you the way... Pinsussyafan28-Feb-05 11:12 
GeneralBarely a beginner's guide... Pinmemberarmentage28-Feb-05 4:51 
GeneralRe: Barely a beginner's guide... Pinmemberjeffr1234520-Mar-06 5:14 
GeneralFactual errors PinmemberDon Clugston27-Feb-05 13:46 

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
Web04 | 2.8.141220.1 | Last Updated 26 Feb 2005
Article Copyright 2005 by Inocentric
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid