首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

问一个单一写入权限,无限个读取权限的锁解决方案

2012-02-17 
问一个单一写入权限,无限个读取权限的锁我写的,感觉很臃肿,大家有没有什么简洁的方法。classFunctionInUseL

问一个单一写入权限,无限个读取权限的锁
我写的,感觉很臃肿,大家有没有什么简洁的方法。
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


热点排行