惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 荣-

学习实践:使用模式,原则实现一个C++自动化测试程序 学习实践:使用模式,原则实现一个C++数据库访问类 C++字符转换等常用方法 DLL内存管理模板类 字符串处理代码(国际化转换C++版) 我的C++数据库访问库 批处理文件的学习 C++中,以类成员函数指针作为参数对std::map中的元素进行迭代处理 取得MySQL数据库表,列信息的SQL语句 我的我的C#数据库操作类(与大家交流) ACE项目的重构整理 安装SQL Server 2000和sp补丁时,安装程序提示"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"。 我的计算机安装步骤 创建oracle用户 我的重构步骤 我的C++代码检查列表 PowerDesigner高级应用 文档的阅读 如何设计类
我的C++数据库访问库--临界区处理类
荣- · 2012-06-17 · via 博客园 - 荣-

在我的C++数据库访问库中,支持线程安全访问,使用临界区处理类实现该功能。

一共有两个类:HisCritical、HisCriticalMng。

HisCritical:主要提供windows临界区的访问,提供构造函数、析构函数、Enter、Leave几个接口。

HisCritical:临界区访问类,主要封装windows临界区的访问,该类主要在栈中使用,利用局部变量的构造和析构函数出入临界区。

下面是代码:

#pragma once

#if defined(_AFXEXT) || defined(_AFXDLL) || _HIS_USE_MFC
#else
#include <Windows.h>
#endif

/**
* @defgroup 临界区域管理
* @brief 临界区与管理,用于添加互斥锁。
* @author 徐敏荣
* @date 2012-06-14
*
* @par 修订历史
* @version v0.5 \n
* @author 徐敏荣
* @date 2012-06-14
* @li 初始版本
* @{
*/
namespace His
{
/**
* @brief 临界区访问类,主要封装windows临界区的访问,该类主要在栈中使用,利用局部变量的构造和析构函数出入临界区
*
*/
class HisCritical
{
public:

/**
* @brief 构造函数
*/
HisCritical()
{
::InitializeCriticalSection(&this->m_cs);
}

/**
* @brief 析构函数
*/
~HisCritical()
{
::DeleteCriticalSection(&this->m_cs);
}

/**
* @brief 进入临界区
*/
void Enter()
{
::EnterCriticalSection(&this->m_cs);
}

/**
* @brief 离开临界区
*/
void Leave()
{
::LeaveCriticalSection(&this->m_cs);
}

private:

/**
* @brief 临界区对象
*/
CRITICAL_SECTION m_cs; /**< 临界区对象 */
};

/**
* @brief 临界区访问管理类,利用构造函数进入临界区,利用西沟函数离开临界区
* 如果向构造函数提供NULL参数,则不使用临界区。
*
*/
class HisCriticalMng
{
public:

/**
* @brief 构造函数
* @param[in] cirtical 临界区访问类
* @note 如果cirtical为空,则不使用临界区
*/
HisCriticalMng(HisCritical* cirtical)
{
this->m_pCritical = cirtical;

if (this->m_pCritical)
{
this->m_pCritical = cirtical;
this->m_pCritical->Enter();
}
}

/**
* @brief 虚构函数
*/
~HisCriticalMng()
{
if (this->m_pCritical)
{
this->m_pCritical->Leave();
}
}

private:
HisCritical* m_pCritical;
};
}

/**//** @}*/ // 临界区域管理