这段代码是一个有效的关键部分包装类吗
我实际上有两个问题,这是第一个问题。
使用我在两个不同站点找到的代码,我编写了这两个关键部分包装类。
它会起作用吗?
#ifndef CRITICALSECTION_H
#define CRITICALSECTION_H
#include "windows.h"
class CriticalSection{
long m_nLockCount;
long m_nThreadId;
typedef CRITICAL_SECTION cs;
cs m_tCS;
public:
CriticalSection(){
::InitializeCriticalSection(&m_tCS);
m_nLockCount = 0;
m_nThreadId = 0;
}
~CriticalSection(){ ::DeleteCriticalSection(&m_tCS); }
void Enter(){ ::EnterCriticalSection(&m_tCS); }
void Leave(){ ::LeaveCriticalSection(&m_tCS); }
void Try();
};
class LockSection{
CriticalSection* m_pCS;
public:
LockSection(CriticalSection* pCS){
m_pCS = pCS;
if(m_pCS)m_pCS->Enter();
}
~LockSection(){
if(m_pCS)m_pCS->Leave();
}
}
/*
Safe class basic structure;
class SafeObj
{
CriticalSection m_cs;
public:
void SafeMethod()
{
LockSection myLock(&m_cs);
//add code to implement the method ...
}
};
*/
#endif
第二个问题。在浏览此处时,我注意到作者做了不包括
::Initialize、Delete、Enter、Leave关键部分。这些不是班级正常运作所需要的吗?或者我错过了什么?
I've actually got 2 questions, heres the first one.
Using the code I found at two different sites, I wrote those two critical section wrapper classes.
Is it going to work?
#ifndef CRITICALSECTION_H
#define CRITICALSECTION_H
#include "windows.h"
class CriticalSection{
long m_nLockCount;
long m_nThreadId;
typedef CRITICAL_SECTION cs;
cs m_tCS;
public:
CriticalSection(){
::InitializeCriticalSection(&m_tCS);
m_nLockCount = 0;
m_nThreadId = 0;
}
~CriticalSection(){ ::DeleteCriticalSection(&m_tCS); }
void Enter(){ ::EnterCriticalSection(&m_tCS); }
void Leave(){ ::LeaveCriticalSection(&m_tCS); }
void Try();
};
class LockSection{
CriticalSection* m_pCS;
public:
LockSection(CriticalSection* pCS){
m_pCS = pCS;
if(m_pCS)m_pCS->Enter();
}
~LockSection(){
if(m_pCS)m_pCS->Leave();
}
}
/*
Safe class basic structure;
class SafeObj
{
CriticalSection m_cs;
public:
void SafeMethod()
{
LockSection myLock(&m_cs);
//add code to implement the method ...
}
};
*/
#endif
And the second question. While browsing here, I noticed that the author did not include
::Initialize , Delete , Enter , Leave critical sections . Aren't these needed for the class to work properly? Or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LockSection
类使用 RAII 调用进入
并离开
。创建对象时,将调用Enter
。当对象被销毁(通过超出范围)时,将调用Leave
。Initialize
和Delete
由CriticalSection
类的构造函数和析构函数调用。学RAII,学好。这是你的朋友。
The
LockSection
class uses RAII to callEnter
andLeave
. When the object is created, theEnter
is called. When the object is destroyed (by going out of scope) theLeave
is called.Initialize
andDelete
are called by theCriticalSection
class' constructor and destructor.Learn RAII, learn it well. It is your friend.
我来回答第一个问题。第二个问题涉及不同站点的代码,您一次只能问一个问题。也许其他人会回答第二个问题。
您包含的代码将正常工作。成员
m_nLockCount
和m_nThreadId
未使用且不需要。Try()
方法没有实现,但您可以将其删除。就个人而言,如果未分配
m_pCS
,我会引发异常。这是一个明显的错误情况。默默地继续并假装资源受到保护是明显的危险。I'll answer the first question. The second question involves code at a different site and you should only ask one question at a time. Perhaps someone else will answer the second question.
The code you included will work correctly. The members
m_nLockCount
andm_nThreadId
are unused and not needed. TheTry()
method has no implementation but you can just delete itPersonally I would raise an exception if
m_pCS
is not assigned. That's a clear error condition. Silently continuing and pretending that the resource is protected is an obvious danger.