线程通信机制之原子操作:Interlock系列函数

线程通信机制之原子操作:Interlock系列函数

——每周杂谈 第007篇

作者:Tocy    时间:2012-05-31

 

关键词:原子操作,Interlock系列函数,ITC

多线程之间共享变量的访问必须做好同步,并且需要保证在某个变量上的操作能够具有原子性(也就是要么该操作不执行,要么该操作完成,不能存在某种中间状态)。

简单的读写32位对齐的变量(四字节)是原子操作,也就是说你不会得到只更新部分比特位的数据,所有的数据都是以原子方式更新的。值得注意的是,数据访问的顺序是不确定的。比如两个线程分别读写同一个变量,是无法确定线程执行读写的顺序的。

在64位操作系统上读写64位数据是原子操作,但是在32位操作系统上读写64位变量就无法保证原子性。在其他平台上都与其他长度的变量的读写通常都不具有原子性。

Windows下提供了Interlocked函数族,来处理多线程之间共享变量访问权带来的冲突问题。这些函数以原子方式执行对变量的操作

1. InterlockedIncrement and InterlockedDecrement函数

++和--的原子操作,参数为变量的地址。

2.  InterlockedExchange and InterlockedExchangePointer函数

以原子方式交换两个变量(或者指针)

3.  InterlockedAndInterlockedOr, and InterlockedXor函数

以原子方式执行与、或、非操作

4. InterlockedExchangeAdd 函数

(原子方式执行相加和将结果保存到某个变量中)

5. InterlockedCompareExchange

InterlockedCompare64Exchange128

InterlockedCompareExchangePointer

以原子方式执行下列操作:比较两个值,并将第三个值按照比较结果保存到其中一个变量中。

6. 在64位操作系统中有专门设计的Interlock函数来处理变量访问。通常这类函数名称均包含"64",例如InterlockedDecrement64 andInterlockedCompareExchangeAcquire64

网址:http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85)

7. Interlocked Singly Linked List互锁单向链表,系统提供针对单向链表的插入和删除操作的同步机制,主要相关函数如下表所示:

函数名

描述

InitializeSListHead

初始化单链表的头结点

InterlockedFlushSList

清空单链表

InterlockedPopEntrySList

移除单链表的第一个结点

InterlockedPushEntrySList

在单链表头部插入新结点。

QueryDepthSList

查询单链表的元素个数

 

MSDN中给出的一个使用例子,如下:

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

}

 

注:版权所有,请勿用于商业用途,转载请注明原文地址。本人保留所有权利。

posted @ 2012-06-03 10:11  Tocy  阅读(1500)  评论(0编辑  收藏  举报