Unix使用互斥量保护数据结构
来源:互联网 发布:淘宝ps卖自己 编辑:程序博客网 时间:2024/04/30 11:24
#include <stdlib.h>
#include <pthread.h>
struct foo
{
int f_count;
pthread_mutex_t f_lock;
/* .. more stuff here ... */
};
struct foo* foo_alloc()
{
struct foo *fp;
if ((fp = (struct foo*)malloc(sizeof(struct foo))) != NULL)
{
fp->f_count = 1;
if (pthread_mutex_init(&fp->f_lock, NULL) != 0)
{
free(fp);
return NULL;
}
/* ... continue initialization ... */
}
return fp;
}
void foo_hold(struct foo *fp) // add a reference to the object
{
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
}
void foo_rele(struct foo *fp) // release a reference to the object
{
pthread_mutex_lock(&fp->f_lock);
if (0 == --fp->f_count) // last reference
{
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_destroy(&fp->f_lock);
free(fp);
}
else
{
pthread_mutex_unlock(&fp->f_lock);
}
}
#include <pthread.h>
struct foo
{
int f_count;
pthread_mutex_t f_lock;
/* .. more stuff here ... */
};
struct foo* foo_alloc()
{
struct foo *fp;
if ((fp = (struct foo*)malloc(sizeof(struct foo))) != NULL)
{
fp->f_count = 1;
if (pthread_mutex_init(&fp->f_lock, NULL) != 0)
{
free(fp);
return NULL;
}
/* ... continue initialization ... */
}
return fp;
}
void foo_hold(struct foo *fp) // add a reference to the object
{
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
}
void foo_rele(struct foo *fp) // release a reference to the object
{
pthread_mutex_lock(&fp->f_lock);
if (0 == --fp->f_count) // last reference
{
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_destroy(&fp->f_lock);
free(fp);
}
else
{
pthread_mutex_unlock(&fp->f_lock);
}
}
0 0
- Unix使用互斥量保护数据结构
- 互斥量(对数据结构保护)
- linux中利用互斥量对数据结构进行保护的源代码
- 使用 molly-guard 保护你的 Linux/Unix 机器不会被错误地关机/重启 | Linux 中国
- 使用电脑 保护眼睛
- 使用const保护数组
- UNIX环境高级编程学习之第十五章进程间通信 - 系统V 共享内存使用(使用信号灯保护共享内存)
- Unix/Linux线程同步互斥量的使用
- 详解UNIX平台下Apache服务的安全保护
- 如何保护你的unix、Linux和mac的servers
- 使用session保护受限页面
- 使用mod_security保护网页服务器
- 使用Tripwire保护Linux文件系统
- 使用protected保护访问方式
- 使用数据库加密保护敏感数据
- 保护模式下ldt使用
- C++ 继承 公有、使用、保护
- 使用shrio保护spring 应用
- python数据挖掘领域工具包
- string类实现
- 指定字符删除
- yaffs2的补丁文件patch-ker分析
- Lua 八皇后问题
- Unix使用互斥量保护数据结构
- 你不在我身边
- 题目1157:中位数:快速排序思想
- 在巨人的肩膀上对软件测试的思考(二)
- Activity中的onNewIntent
- c++基础部分(1)
- 有才的美国人
- 乱码新解
- php生成短网址示例代码