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

链表,配合critical section

时间:2017-06-30 19:54:16      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:post   eof   一个   malloc   同步   bsp   list   node   sleep   

#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

标签:post   eof   一个   malloc   同步   bsp   list   node   sleep   

原文地址:http://www.cnblogs.com/jzdwajue/p/7100334.html

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