# Include
<Windows. h>
Class creadwritelock
{
PRIVATE:
Long
Mdwreaders;
Critical_section menterlock;
Public:
Creadwritelock (void );
~ Creadwritelock (void );
Bool
Readlock ();
Bool
Readunlock ();
Bool
Writelock ();
Bool
Writeunlock ();
};
/*************************************** ************
Creadwritelock: creadwritelock (void)
Constructor
**************************************** *************/
Inline creadwritelock: creadwritelock (void)
{
Initializecriticalsection (& menterlock );
Mdwreaders =
0;
# Ifdef
_ Fan_debug __
STD: cout <"create readwritelock" <Endl;
# Endif
// _ Fan_debug __
}
/*************************************** ************
Creadwritelock ::~ Creadwritelock (void)
Destructor
**************************************** *************/
Inline creadwritelock ::~ Creadwritelock (void)
{
Deletecriticalsection (& menterlock );
# Ifdef
_ Fan_debug __
STD: cout <"delete readwritelock" <Endl;
# Endif
// _ Fan_debug __
}
/*************************************** ************
Creadwritelock: readlock ()
Read lock
Returns true.
Locked
False
Other cases
**************************************** *************/
Inline bool creadwritelock: readlock ()
{
Entercriticalsection (& menterlock );
Interlockedincrement (& mdwreaders );
Leavecriticalsection (& menterlock );
# Ifdef
_ Fan_debug __
STD: cout <"Reader lock success." <Endl;
# Endif
// _ Fan_debug __
Return
True;
}
/*************************************** ************
Creadwritelock: readunlock ()
Read unlock
Returns true.
Locked
False
Other cases
**************************************** *************/
Inline bool creadwritelock: readunlock ()
{
Interlockeddecrement (& mdwreaders );
# Ifdef
_ Fan_debug __
STD: cout <"Reader unlock success." <Endl;
# Endif
// _ Fan_debug __
Return
True;
}
/*************************************** ************
Creadwritelock: writelock ()
Write lock
Returns true.
Locked
False
Other cases
**************************************** *************/
Inline bool creadwritelock: writelock ()
{
// Handle
Mhthread = getcurrentthread ();
// Bool
Mbsetprioritysuccess = false;
Entercriticalsection (& menterlock );
// Until all read users exit
// If
(Mdwreaders> 0)
//{
//
Mbsetprioritysuccess =
//
Setthreadpriority (mhthread, thread_priority_lowest );
//}
While (mdwreaders> 0)
{
# Ifdef
_ Fan_debug __
STD: cout <"writer lock test Reader
Number. "<Endl;
STD: cout <"current Reader number is
"<Mdwreaders <Endl;
# Endif
// _ Fan_debug __
Sleep (0 );
}
// If
(Mbsetprioritysuccess)
//{
//
Setthreadpriority (mhthread, thread_priority_normal );
//}
# Ifdef
_ Fan_debug __
STD: cout <"writer lock success." <Endl;
# Endif
// _ Fan_debug __
Return
True;
}
/*************************************** ************
Creadwritelock: writeunlock ()
Write unlock
Returns true.
Locked
False
Other cases
**************************************** *************/
Inline bool creadwritelock: writeunlock ()
{
Leavecriticalsection (& menterlock );
# Ifdef
_ Fan_debug __
STD: cout <"writer unlock success." <Endl;
# Endif
// _ Fan_debug __
Return
True;
}