SRWLOCK?? G_lock?=? {};???????? Lightweight lock condition_variable?? g_val={};//the synchronization condition of the lightweight lock int?? iglobal?=?0;???????????? Read-Write global variable DWORD?? WINAPI?? Writethread (Lpvoid?lpparameter) {//write thread while? 1) {?? __try{acquiresrwlockexclusive (&g_lock);//apply for exclusive lock iglobal++;} __finally{releasesrwlockexclusive (&g_lock);//Release exclusive lock wakeallconditionvariable (&g_val);//wake-up condition variable}}return?0;} DWORD? WINAPI?? Readthread (Lpvoid?lpparameter) {//Read thread while? true) {??? Sleep (+); __try{acquiresrwlockshared (&g_lock);//apply for shared lock bool?rt?=? SLEEPCONDITIONVARIABLESRW (&g_val,?&g_lock,?20,?????????????????????? condition_variable_lockmode_shared);//wait for the condition variable if? (RT) {char?str[20]?=? {};itoa (iglobal,?str,?10); TRACE (str);}? Else? {char?str[20]?=? {};itoa (iglobal,?str,?10); TRACE (str);}} __finally{releasesrwlockshared (&g_lock);//release lock}}return?0;} void? Ctestthreaddlg::onbnclickedbutton15 () {initializesrwlock (&g_lock); initializeconditionvariable (&g_val);D Word?? threadid?=0; HANDLE?? Hwrite?=? CreateThread (null,?0,? ( Lpthread_start_routine) WritethreAd,?????????????????????? Null? Create_suspended,?&threadid); HANDLE?? Hread?? =? CreateThread (null,?0,? ( Lpthread_start_routine) Readthread,?????????????????????? Null? Create_suspended,?&threadid); ResumeThread (Hwrite); ResumeThread (hread);}
Use SRW lightweight lock sharing variables between threads