実際に2つの質問があります。最初の質問です。
2つの異なるサイトで見つけたコードを使用して、これら2つのクリティカルセクションラッパークラスを作成しました。
それはうまくいくでしょうか?
#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
そして2番目の質問。ここを閲覧していると、作者が含まれていないことに気づきました
::初期化、削除、入力、クリティカルセクションを残す。クラスが正しく機能するためにこれらは必要ではありませんか?それとも私は何かが足りないのですか?