Implementation of a simple read/write lock (Windows)

Source: Internet
Author: User

# 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.