一点想法:
用两个Event,aEvent和bEvent(用CreateEvent()创建)
a,b分别等自己的Event到达,用WaitForSingleObject(),
c用WaitForMultipleObjects()等aEvent,bEvent到达
用两个Event,aEvent和bEvent(用CreateEvent()创建)
a,b分别等自己的Event到达,用WaitForSingleObject(),
c用WaitForMultipleObjects()等aEvent,bEvent到达
{
public:
bool UseC(bool buse);
bool UseA(bool buse);
bool UseB(bool buse);
CLock();
virtual ~CLock();protected:
bool m_bAUse;
bool m_bBUse;
bool m_bCUse;
CCriticalSection m_cs;
};
//====================cpp=====================
CLock::CLock()
{
m_bAUse =false;
m_bBUse= false;
m_bCUse =false;
}CLock::~CLock()
{}
//before A use S, call CLock::UseA(true), if return true, A can use S
//if A release S, call CLock::UseA(false), this call always return true.
bool CLock::UseA(bool buse)
{
bool bret;
if(buse)
{
m_cs.Lock();
if(m_bCUse)//C is accessing S, A can not access S
{
bret=false;
}
else
{
m_bAUse=true;
bret = true;
}
m_cs.Unlock();
return bret;
}
else
{
m_cs.Lock();
m_bAUse=false;
m_cs.Unlock();
return true;
}
}bool CLock::UseB(bool buse)
{
bool bret;
if(buse)
{
m_cs.Lock();
if(m_bCUse)//C is accessing S, A can not access S
{
bret=false;
}
else
{
m_bBUse=true;
bret = true;
}
m_cs.Unlock();
return bret;
}
else
{
m_cs.Lock();
m_bBUse=false;
m_cs.Unlock();
return true;
}}bool CLock::UseC(bool buse)
{
bool bret;
if(buse)
{
m_cs.Lock();
if(m_bBUse||m_bAUse)
bret=false;
else
{
bret=true;
m_bCUse=true;
}
m_cs.Unlock();
return bret;
}
else
{
m_cs.Lock();
m_bCUse=false;
m_cs.Unlock();
return true;
}}