Win32 classic code: use key segments to implement read/write locks

Source: Internet
Author: User

 

Class creadwritesection <br/>{< br/> Public: <br/> creadwritesection () {<br/> initializecriticalsection (& _ csenterread ); <br/> initializecriticalsection (& _ cswriter); <br/> _ lreaders =-1; <br/>}< br/> ~ Creadwritesection () {<br/> deletecriticalsection (& _ csenterread); <br/> deletecriticalsection (& _ cswriter); <br/>}< br/> void enterread () <br/>{< br/> entercriticalsection (& _ csenterread); <br/> If (! Interlockedincrement (& _ lreaders) <br/> entercriticalsection (& _ cswriter); <br/> leavecriticalsection (& _ csenterread ); <br/>}< br/> void leaveread () <br/> {<br/> If (interlockeddecrement (& _ lreaders) <0) <br/> leavecriticalsection (& _ cswriter); <br/>}< br/> void enterwrite () <br/>{< br/> entercriticalsection (& _ csenterread ); <br/> entercriticalsection (& _ cswriter); <br/>}< br/> void leavewrite () <br/ >{< Br/> leavecriticalsection (& _ cswriter); <br/> leavecriticalsection (& _ csenterread ); <br/>}< br/> // note that readers will continue reading underneath this critical section <br/> critical_section * getpcs () <br/>{< br/> return & _ csenterread; <br/>}< br/> PRIVATE: <br/> critical_section _ csenterread; <br/> critical_section _ cswriter; <br/> long _ lreaders; <br/>}; <br/> class creadlock <br/> {<br /> Public: <br/> creadlock (creadwritesection & crws) {(_ prws = & crws)-> enterread () ;}< br/> ~ Creadlock () {_ prws-> leaveread () ;}< br/> PRIVATE: <br/> creadwritesection * _ prws; <br/> }; <br/> class cwritelock <br/> {<br/> Public: <br/> cwritelock (creadwritesection & crws) {(_ prws = & crws) -> enterwrite () ;}< br/> ~ Cwritelock () {_ prws-> leavewrite () ;}< br/> PRIVATE: <br/> creadwritesection * _ prws; <br/> }; <br/> # define lock_read (crws) creadlock crws # _ readlock (crws) <br/> # define lock_write (crws) cwritelock crws # _ writelock (crws) 

 

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.