链表,配合critical section
2015-07-24 09:29
302 查看
#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;
}
![](http://img.blog.csdn.net/20150724091748351?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150724091759899)
![](http://img.blog.csdn.net/20150724091934269)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/panic.gif)
最小锁定时间
同步机制中,不要长时间锁住一份资源。
警告:千万不要在一个cirtical section 之中调用Sleep() 或任何Wait() API函数。
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;
}
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/panic.gif)
最小锁定时间
同步机制中,不要长时间锁住一份资源。
警告:千万不要在一个cirtical section 之中调用Sleep() 或任何Wait() API函数。
相关文章推荐
- 一个用户加入多个组
- 【system】利用Diskpart命令(cmd)解决装机分区格式不对,增大C盘空间问题
- 应用程序配置文件中的连接字符串不包含必需的 providerName 特性
- 事情高大谈话(智能家居)技术和平台
- Android开发学习笔记二Android Studio
- 搭建高可用的MongoDB集群(上):MongoDB的配置与副本集----- 个人记录
- 简单的新闻列表鼠标移入背景变色功能
- 图像处理------快速均值模糊(Box Blur)
- 图像处理------快速均值模糊(Box Blur)
- Linux下进程绑定多CPU运行
- 用结构体变量和指向结构体变量的指针构成链表
- Spring读取配置文件,获取bean的几种方式
- 图像处理------快速均值模糊(Box Blur)
- 使用su后如何返回原来的用户
- UVA 810 - A Dicey Problem(BFS)
- Log4Net使用指南(转)
- 图论:匹配与覆盖+独立集 团与支配集
- 图像处理------快速均值模糊(Box Blur) 分类: 视频图像处理 2015-07-24 09:28 30人阅读 评论(0) 收藏
- hihoCoder #1051 补提交卡
- .net面向对象之多线程(Multithreading)及 多线程高级应用