码迷,mamicode.com
首页 > 其他好文 > 详细

链表,配合critical section

时间:2015-07-24 10:54:14      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

#include <windows.h>
typedef struct _Node
{
struct _Node *next;
int data;
} Node;


typedef struct _List
{
Node *head;
CRITICAL_SECTION critical_sec;


} List;
List *CreateList()
{
List *pList = (List*)malloc(sizeof(List));
pList->head = NULL;
InitializeCriticalSection(&pList->critical_sec);
return pList;
}
void DeleteList(List *pList)
{
DeleteCriticalSection(&pList->critical_sec);
free(pList);
}
void AddHead(List *pList, Node *node)
{
EnterCriticalSection(&pList->critical_sec);
node->next = pList->head;
pList->head = node;
LeaveCriticalSection(&pList->critical_sec); 
}
void Insert(List *pList, Node *afterNode, Node *newNode)
{
EnterCriticalSection(&pList->critical_sec); 
if (afterNode == NULL)
{
AddHead(pList, newNode);
}
else
{
newNode->next = afterNode->next;
afterNode->next = newNode;
}
LeaveCriticalSection(&pList->critical_sec);
}
Node *Next(List *pList, Node *node)
{
Node* next;
EnterCriticalSection(&pList->critical_sec);
next = node->next;
LeaveCriticalSection(&pList->critical_sec);
return next;

}

技术分享技术分享技术分享

技术分享最小锁定时间

      同步机制中,不要长时间锁住一份资源。

警告:千万不要在一个cirtical section 之中调用Sleep() 或任何Wait() API函数。


版权声明:本文为博主原创文章,未经博主允许不得转载。

链表,配合critical section

标签:

原文地址:http://blog.csdn.net/wangfengfan1/article/details/47035687

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!