SLists使用了无锁算法来保证原子同步,以提升系统性能,避免了诸如优先级挂和互锁的问题。

注意:所有的链表项必须对齐到MEMORY_ALLOCATION_ALIGNMENT。否则会出现奇葩的错误。

(PS:看英文MSDN的API解释,我感觉这是从前插又从前取,是个栈的样子。)

InitializeSListHead,创建一个空栈

void WINAPI InitializeSListHead(
__inout PSLIST_HEADER ListHead//SLIST_HEADER类型的链头,供系统使用。
);

InterlockedFlushSList,清空栈(感觉这个返回值没什么用的样子,难道可以先全部得到,然后根据链表中元素数量再一个一个地用?求解释。)

PSLIST_ENTRY WINAPI InterlockedFlushSList(
__inout PSLIST_HEADER ListHead//创建空栈时用的那个链头
);

InterlockedPushEntrySList,在头添加(区别于从尾部添加),返回值为之前的第一项,如果之前为空链,则返回NULL。

PSLIST_ENTRY WINAPI InterlockedPushEntrySList(
__inout PSLIST_HEADER ListHead,//创建空栈时用的那个链头
__inout PSLIST_ENTRY ListEntry//插入项
);

InterlockedPopEntrySList,在头取出(区别于从尾部取出),返回值就是取的那个项的指针,如果之前为空链,则返回NULL。

PSLIST_ENTRY WINAPI InterlockedPopEntrySList(
__inout PSLIST_HEADER ListHead
);

QueryDepthSList,返回元素的数量。

USHORT WINAPI QueryDepthSList(
__in PSLIST_HEADER ListHead
);

微软的MSDN上有个例子,我复制了下来,链接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms686962。代码如下。

#include <windows.h>
#include <malloc.h>
#include <stdio.h> // Structure to be used for a list item; the first member is the
// SLIST_ENTRY structure, and additional members are used for data.
// Here, the data is simply a signature for testing purposes. typedef struct _PROGRAM_ITEM {
SLIST_ENTRY ItemEntry;
ULONG Signature;
} PROGRAM_ITEM, *PPROGRAM_ITEM; int main( )
{
ULONG Count;
PSLIST_ENTRY pFirstEntry, pListEntry;
PSLIST_HEADER pListHead;
PPROGRAM_ITEM pProgramItem; // Initialize the list header to a MEMORY_ALLOCATION_ALIGNMENT boundary.
pListHead = (PSLIST_HEADER)_aligned_malloc(sizeof(SLIST_HEADER),
MEMORY_ALLOCATION_ALIGNMENT);
if( NULL == pListHead )
{
printf("Memory allocation failed.\n");
return -1;
}
InitializeSListHead(pListHead); // Insert 10 items into the list.
for( Count = 1; Count <= 10; Count += 1 )
{
pProgramItem = (PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM),
MEMORY_ALLOCATION_ALIGNMENT);
if( NULL == pProgramItem )
{
printf("Memory allocation failed.\n");
return -1;
}
pProgramItem->Signature = Count;
pFirstEntry = InterlockedPushEntrySList(pListHead,
&(pProgramItem->ItemEntry));
} // Remove 10 items from the list and display the signature.
for( Count = 10; Count >= 1; Count -= 1 )
{
pListEntry = InterlockedPopEntrySList(pListHead); if( NULL == pListEntry )
{
printf("List is empty.\n");
return -1;
} pProgramItem = (PPROGRAM_ITEM)pListEntry;
printf("Signature is %d\n", pProgramItem->Signature); // This example assumes that the SLIST_ENTRY structure is the
// first member of the structure. If your structure does not
// follow this convention, you must compute the starting address
// of the structure before calling the free function. _aligned_free(pListEntry);
} // Flush the list and verify that the items are gone.
pListEntry = InterlockedFlushSList(pListHead);
pFirstEntry = InterlockedPopEntrySList(pListHead);
if (pFirstEntry != NULL)
{
printf("Error: List is not empty.\n");
return -1;
} _aligned_free(pListHead); return 1;
}

这里略微解释下吧!

微软自己定义的那个结构体PROGRAM_ITEM的第一项是SLIST_ENTRY类型的变量。

往InterlockedPushEntrySList和InterlockedPopEntrySList传递时传递的是自定义结构体的第一项。

结构体的地址和它的第一项的地址是同一个。

转载请注明出处http://blog.csdn.net/wlsgzl/article/details/17021551

读书笔记——Windows核心编程(8)Interlocked单向链式栈的更多相关文章

  1. 读书笔记——Windows核心编程(8)Interlocked系列函数

    先让我们来复习下小学知识 A+B=C//式中A为被加数,B为加数. A-B=C//式中A为被减数,B为减数. 再让我们来明确一个知识点:返回值为void的Windows函数意味着一定会执行成功. -- ...

  2. 读书笔记——Windows核心编程(2)比较字符串

    1. CompareString 以符合用户语言习惯的方式,EX版本使用UNICODE int CompareString( __in LCID Locale, __in DWORD dwCmpFla ...

  3. 读书笔记——Windows核心编程(15)在应用程序中使用虚拟内存

    微软的Windows提供了三种机制对内存进行操控 1 虚拟内存(最适合管理大型对象数组或大型结构数组) 2 内存映射文件(大型数据流/文件,共享数据) 3  堆(大量的小型对象) 预订地址空间区域Vi ...

  4. 读书笔记——Windows核心编程(13)Windows内存体系结构

    对于32位进程(0x0000 0000~0xFFFF FFFF),有4GB的地址空间. 每个进程都有自己专有的地址空间,当进程的各个线程运行时,它们只能访问属于该进程的内存. 这4GB其实是虚拟地址空 ...

  5. 读书笔记——Windows核心编程(2)禁止C运行时触发的所有Debug Assertion Failed对话框

    1 定义一个函数 void _invalid_parameter( const wchar_t * expression, const wchar_t * function, const wchar_ ...

  6. 《Windows核心编程》读书笔记 上

    [C++]<Windows核心编程>读书笔记 这篇笔记是我在读<Windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的思考和对 ...

  7. C++Windows核心编程读书笔记

    转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%96%87/71405.shtml "C++Windows核心编程读书笔 ...

  8. 【转】《windows核心编程》读书笔记

    这篇笔记是我在读<Windows核心编程>第5版时做的记录和总结(部分章节是第4版的书),没有摘抄原句,包含了很多我个人的思考和对实现的推断,因此不少条款和Windows实际机制可能有出入 ...

  9. windows核心编程 - 线程同步机制

    线程同步机制 常用的线程同步机制有很多种,主要分为用户模式和内核对象两类:其中 用户模式包括:原子操作.关键代码段 内核对象包括:时间内核对象(Event).等待定时器内核对象(WaitableTim ...

随机推荐

  1. java switch语句注意的事项

    1.switch语句使用的变量只能是byte.char.short.string数据类型. 2.case后面gender数据必须是一个常量. 3.switch的停止条件: switch语句一旦比配上了 ...

  2. visual studio 局域网远程调试web项目

    1.进入项目根目录,找到.vs/config/applicationhost.config文件(可能是隐藏的) 2.搜索sites节点,找到当前项目,并添加一个binding配置节,将ip地址设置为本 ...

  3. Asp.net 字符(二)

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  4. 最新的SqlHelper 类

    最新的SqlHelper 类 摘自:http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html using System; using S ...

  5. C#组态控件Iocomp应用案例

    Iocomp组件需要在vs2010环境下使用,目前用到的是4.04版本.在两个项目中用到了它,一个是锅炉监控系统,另一个是绝缘靴检测系统. 锅炉监测系统 这个节目基本都是使用Iocomp控件完成. 出 ...

  6. 关于SVN删除后的文件不能重新添加(正常途径不行)

    在你自己的机器上(即SVN客户端),把“新建test”文件夹标记为删除,然后提交,在删除之前可以备份“新建test”文件夹,提交后,在当前文件夹下更新SVN.然后把你刚刚备份的文件夹重新放到该目录下. ...

  7. learning sql (second edition) script

    create database bank; use bank; /* begin table creation */ create table department (dept_id smallint ...

  8. parseInt第二个参数详解

    前阵子在stackOverflow上看到两个这样的问题: 为什么parseInt(8,3) == NaN,parseInt(16,3) == 1? 为什么parseInt('dsff66',16) = ...

  9. js 操作ASP.NET服务器控件

    js 操作ASP.NET服务器控件 在ASP.NET中使用js时,js获取DOM元素时,经常获取不到,这是因为获取的方法有误,现在介绍一方法,解决如何使用js获取ASP.NET控件在浏览器端生成htm ...

  10. DAX 2009 for Retail's P job does not work after restoring AX database from another environment.

    This time, it's P job.  We already re-configured profiles, distribution locations, distribution grou ...