读书人

问一个单一写入权限无限个读取权限的

发布时间: 2012-02-17 17:50:42 作者: rapoo

问一个单一写入权限,无限个读取权限的锁
我写的,感觉很臃肿,大家有没有什么简洁的方法。
class FunctionInUseLock
{
protected:
bool in_exclude_Lock;
CMutex exlockMutex;
int comment_use_count;
CMutex comlockMutex;
HANDLE StateEvent[2];
public:
FunctionInUseLock():in_exclude_Lock(false),comment_use_count(0)
{
StateEvent[0]=CreateEvent(NULL,TRUE,TRUE,NULL);//comment
StateEvent[1]=CreateEvent(NULL,TRUE,TRUE,NULL);//exclude
}
~FunctionInUseLock()
{
if(StateEvent[0]!=NULL)
CloseHandle(StateEvent[0]);
if(StateEvent[1]!=NULL)
CloseHandle(StateEvent[1]);
}
inline void CommentUseLock()
{
::WaitForSingleObject(StateEvent[1],INFINITE);
ResetEvent(StateEvent[0]);
comlockMutex.Lock();
comment_use_count++;
comlockMutex.Unlock();
}
inline void ExcludeUseLock()
{
WaitForMultipleObjects(2,StateEvent,TRUE,INFINITE);
ResetEvent(StateEvent[1]);
exlockMutex.Lock();
in_exclude_Lock=true;
exlockMutex.Unlock();
}
inline void Unlock()
{
exlockMutex.Lock();
if(in_exclude_Lock==false)
{
exlockMutex.Unlock();
comlockMutex.Lock();
comment_use_count--;
if(comment_use_count==0)
{
comlockMutex.Unlock();
SetEvent(StateEvent[0]);
return;
}
comlockMutex.Unlock();
}
else
{
in_exclude_Lock=false;
exlockMutex.Unlock();
SetEvent(StateEvent[1]);


}
}
};

[解决办法]
读锁。写锁问题

臃肿? 把类型的实现代码放到外面

// h file
delare the class

// cpp file
//implement the class


读书人网 >C++

热点推荐