Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / ATL
Article

Hardwired's Thread Safe Double Link List Template

Rate me:
Please Sign up or sign in to vote.
1.24/5 (16 votes)
27 Feb 20052 min read 35K   354   16   6
This article explains how to use CLinkedListDouble template class. This template is a thread safe implementation of double link list.

Introduction

As promised in Single Link List article, here is the implementation of Double Link List, in thread safe way. This article is quite similar to the one I've already mentioned.

You might find it useful to your applications.

Using the code

First, add everything that's in the pack to your project:

  • LinkedListDouble.h
  • LinkedListDouble.cpp
  • NamedCriticalSection.h
  • NamedCriticalSection.cpp
  • SpinLock.h
  • SpinLock.cpp
  • DebugTrace.h
  • DebugTrace.cpp

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

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

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

//
// somewhere in your application:
//

CLinkedListDouble<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:
//

CLinkedListDouble<SomeClass, sinzeof(SomeClass)>    SomeClassList;

CLinkedListDouble

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

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

    USHORT        Size;        // size of list

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

    CLinkedListDouble();
    ~CLinkedListDouble();

    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 may not be of your interest. 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:
//

CLinkedListDouble<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 double linked lists.

One more thing...

I'm not interested in ratings, edited article or any article competition. I never took in consideration any ratings when reading an article or downloading. I will not bother to answer messages like: "There is already an article like this here, and it's better...".

I just post here because CodeProject really helped me over the years, so I want to give back something. If something like this already exists here, on CodeProject... I do not really care. Use the one you want - it's your choice.

Anyway... I'll continue to post here!

History

  • 28-february-2005

    version 1.0

    As promised, I've post it this one, too.

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
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.
This is a Organisation (No members)


Comments and Discussions

 
GeneralImplementation facts Pin
Ugluk27-Jul-05 2:42
Ugluk27-Jul-05 2:42 
GeneralRe: Implementation facts Pin
Inocentric27-Jul-05 2:48
Inocentric27-Jul-05 2:48 
GeneralSpare us that! Pin
Alexander M.,1-Mar-05 3:05
Alexander M.,1-Mar-05 3:05 
GeneralGood to know ... Pin
CP Visitor28-Feb-05 22:31
CP Visitor28-Feb-05 22:31 
GeneralRe: Good to know ... Pin
Kandjar2-Mar-05 15:39
Kandjar2-Mar-05 15:39 
He probably wants to have the platinium rank on CodeProject. So he needs to release a big amount of articles (no matter the quality). Smile | :)

Generalwaste of our time Pin
Anonymous28-Feb-05 14:43
Anonymous28-Feb-05 14:43 

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.