Synchronization of read/write locks and condition variables by threads in Windows core programming user mode

Source: Internet
Author: User

Read/write lock

# Include <windows. h> # include <tchar. h> # include <stdio. h> # include <process. h> # include <WINBASE. h> // read/write lock srwlock; unsigned winapi readthread1 (lpvoid) {While (1) {acquiresrwlockshared (& srwlock); printf ("read 1111 \ n "); releasesrwlockshared (& srwlock); sleep (500);} return 0;} unsigned winapi readthread2 (lpvoid) {While (1) {acquiresrwlockshared (& srwlock ); printf ("read 2222222222 \ n"); releasesrwlockshared (& srwlock); sleep (500);} return 0;} unsigned winapi writethread (lpvoid) {While (1) {acquiresrwlockexclusive (& srwlock); printf ("write \ n"); releasesrwlockexclusive (& srwlock); sleep (1000);} return 0;} int main () {initializesrwlock (& srwlock); closehandle (handle) _ beginthreadex (null, 0, readthread1, null, 0, null); closehandle (handle) _ beginthreadex (null, 0, readthread2, null, 0, null); closehandle (handle) _ beginthreadex (null, 0, writethread, null, 0, null )); system ("pause"); Return 0 ;}

Condition variable

Sleepconditionvariablesrw can be called recursively, but it must be released once.

# Include <windows. h> # include <tchar. h> # include <stdio. h> # include <process. h> # include <WINBASE. h> // read/write locks and condition variables // key segments and condition variables critical_section g_cs; condition_variable g_conditionvariable; unsigned winapi testcs (lpvoid) {entercriticalsection (& g_cs ); printf ("11 \ n"); sleepconditionvariablecs (& g_conditionvariable, & g_cs, infinite); printf ("AAAA \ n"); leavecriticalsection (& g_cs); Return 0 ;} unsigned winapi ttttttt (lpvoid) {entercriticalsection (& g_cs); printf ("2222 \ n"); printf ("333 \ n"); leavecriticalsection (& g_cs ); wakeconditionvariable (& g_conditionvariable); Return 0 ;}int main () {condition (& g_cs); initializeconditionvariable (& g_conditionvariable); handle hthread = (handle) _ beginthreadex (null, 0, testcs, null, 0, null); handle hthread1 = (handle) _ beginthreadex (null, 0, ttttttt, null, 0, null); System ("pause "); return 0 ;}

# Include <windows. h> # include <tchar. h> # include <stdio. h> # include <process. h> # include <WINBASE. h> // read/write lock and condition variable sharing mode srwlock; // condition variable condition_variable condition_var; unsigned winapi readthread1 (lpvoid) {While (1) {acquiresrwlockshared (& srwlock ); printf ("thread 1 prepare the waiting condition variable \ n"); sleepconditionvariablesrw (& condition_var, & srwlock, infinite, condition_variable_lockmode_shared); printf ("thread 1 reaches \ n "); releasesrwlockshared (& srwlock); sleep (500);} return 0;} unsigned winapi readthread2 (lpvoid) {While (1) {acquiresrwlockshared (& srwlock ); printf ("thread 2 prepare the waiting condition variable \ n"); sleepconditionvariablesrw (& condition_var, & srwlock, infinite, condition_variable_lockmode_shared); printf ("thread 2 reaches \ n "); releasesrwlockshared (& srwlock); sleep (500);} return 0;} unsigned winapi writethread (lpvoid) {While (1) {acquiresrwlockexclusive (& srwlock ); printf ("write \ n"); releasesrwlockexclusive (& srwlock); sleep (1000);} return 0;} int main () {initializesrwlock (& srwlock ); initializeconditionvariable (& condition_var); closehandle (handle) _ beginthreadex (null, 0, readthread1, null, 0, null); closehandle (handle) _ beginthreadex (null, 0, readthread2, null, 0, null); // closehandle (// (handle) _ beginthreadex (null, 0, writethread, null, 0, null )); // sleep (2000); printf ("Prepare activation condition variable \ n"); // if no conditional variable is waiting during activation, the activation status will be lost, that is to say, activation is only available when waiting for activation (& condition_var); sleep (100); printf ("Main over \ n"); System ("pause "); return 0 ;}

The following Code introduces the use of read/write locks and conditional variables.

# Include <windows. h> # include <tchar. h> # include <stdio. h> # include <process. h> # include <WINBASE. h> // read/write lock and condition variable sharing mode // Apple count long volatile gl_apple = 0; srwlock; // condition variable condition_variable cv_eat; condition_variable cv_make; # define eattime 100 # define maketime 200 void printapple () {printf ("% d Apple \ n", gl_apple);} // eat apple unsigned winapi alleneatapple (lpvoid) {While (1) {acquiresrwlockshared (& srwlock); printf ("Allen is going to eat apple \ n"); While (interlockedcompareexchange (& gl_apple, 0, 0) <= 0) {printf ("Allen has no apple, wait \ n"); sleepconditionvariablesrw (& cv_eat, & srwlock, infinite, condition_variable_lockmode_shared);} interlockeddecrement (& gl_apple ); printf ("Allen finished \ n"); printapple (); releasesrwlockshared (& srwlock); wakeconditionvariable (& cv_make); printf ("Allen say: Make apple \ n "); sleep (eattime);} return 0;} unsigned winapi limeieatapple (lpvoid) {While (1) {acquiresrwlockshared (& srwlock); printf ("limei ready to eat apple \ n "); while (interlockedcompareexchange (& gl_apple, 0, 0) <= 0) {printf ("limei has no apple, etc \ n"); sleepconditionvariablesrw (& cv_eat, & srwlock, infinite, condition) ;}interlockeddecrement (& gl_apple); printf ("limei finished \ n"); printapple (); releasesrwlockshared (& srwlock); wakeconditionvariable (& cv_make ); printf ("limei say: Make apple \ n"); sleep (eattime);} return 0;} unsigned winapi makeapple (lpvoid) {While (1) {acquiresrwlockexclusive (& srwlock); // only 10 if (interlockedcompareexchange (& gl_apple, 10, 10)> = 10) can be produced {// The exclusive lock waits for consumption of printf ("enough, \ n "not produced); sleepconditionvariablesrw (& cv_make, & srwlock, infinite, 0);} interlockedincrement (& gl_apple); printf (" One is produced, and there are currently: % d \ n ", gl_apple); releasesrwlockexclusive (& srwlock); wakeallconditionvariable (& cv_eat); // tell them that they can have sleep (maketime);} return 0 ;} int main () {initializesrwlock (& srwlock); initializeconditionvariable (& cv_eat); week (& cv_make); closehandle (handle) _ beginthreadex (null, 0, alleneatapple, null, 0, null); closehandle (handle) _ beginthreadex (null, 0, limeieatapple, null, 0, null); closehandle (handle) _ beginthreadex (null, 0, makeapple, null, 0, null); System ("pause"); Return 0 ;}

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.