65.9K
CodeProject 正在变化。 阅读更多。
Home

Hardwired 的线程安全双向链表模板

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.24/5 (12投票s)

2005年2月28日

2分钟阅读

viewsIcon

35848

downloadIcon

354

本文介绍了如何使用 CLinkedListDouble 模板类。此模板是双向链表的线程安全实现。

引言

正如在单向链表文章中承诺的那样,这里是双向链表的实现,采用线程安全的方式。本文与我已经提到的文章非常相似。

您可能会发现它对您的应用程序有用。

使用代码

首先,将包中的所有内容添加到您的项目中

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

其次,在需要声明新列表的地方包含它

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

完成所有这些操作后,像这样声明一个新列表

//
// somewhere in your application:
//

CLinkedListDouble<int, sinzeof(int)>   IntegerList;

该模板可以用于任何东西……所以,让我们把事情搞复杂一点。假设我们有这个类

//
// 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++; };    
}

现在,您想要一个 SomeClass 元素的列表。 您可以像这样声明它

//
// somewhere in your application:
//

CLinkedListDouble<SomeClass, sinzeof(SomeClass)>    SomeClassList;

CLinkedListDouble

这不是一个复杂的类......一旦你了解它了:)。

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
};

通常,您可能对此不感兴趣。此类旨在“设置并使用它”。

还有一件事。如果您需要直接访问列表的元素 - 假设您需要“遍历”所有元素并执行某些操作 - 请记住这样做

//
// 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();
}

关注点

任何涉及双向链表的事情。

还有一件事...

我对评分、编辑文章或任何文章竞赛不感兴趣。我在阅读或下载文章时从未考虑过任何评分。我不会费心回复诸如“这里已经有一篇类似的文章,而且更好……”之类的消息。

我只是在这里发布,因为 CodeProject 多年来确实帮助了我,所以我想回馈一些东西。如果 CodeProject 上已经存在类似的东西……我真的不在乎。使用您想要的那个 - 这是您的选择。

无论如何...我会继续在这里发帖!

历史

  • 2005 年 2 月 28 日

    版本 1.0

    正如承诺的那样,我也发布了这一个。

© . All rights reserved.