20. Event Communication Implementation process repeater mode call

Source: Internet
Author: User

    • Run
    •  

    • Create event
       1  HANDLE event  [4 ] = {0 };//  event  

       

    • Create thread
       Span style= "COLOR: #008080" >1  HANDLE hthread[3 ] = {0 }; //  represents thread  

       

    • Create critical section
       critical_section CS; 

       

    • Create chat buffer
       1  volatile  char  str[1024 ] = {0  }; //  represents the buffer for chat content  

       

Full code

1 #define_crt_secure_no_warnings2#include <Windows.h>3#include <stdio.h>4#include <stdlib.h>5#include <memory.h>6 7 //Events8HANDLEEvent[4] = {0 };9 //ThreadsTenHANDLE hthread[3] = {0 }; One //Create a critical section A critical_section CS1; -  - Charstr[1024x768] = {0};//the buffer that represents the chat content the 
Middle person -DWORD WINAPI Zhong (void*p) - { - inti =0; + while(++i) - { + //wait for the boy to send event 0 to continue execution AWaitForSingleObject (Event[0], INFINITE); atResetEvent (Event[0]); -EnterCriticalSection (&CS1); -printf"%s\n", str); -LeaveCriticalSection (&CS1); - - //Send Event 1 inSetEvent (Event[1]); - to + //wait for event 2 sent by girl to continue execution -WaitForSingleObject (Event[2], INFINITE); theResetEvent (Event[2]); *EnterCriticalSection (&CS1); $printf"%s\n", str);Panax NotoginsengLeaveCriticalSection (&CS1); - //Send Event 3 theSetEvent (Event[3]); + ASleep ( +); the } + } - $DWORD WINAPI Boy (void*p) $ { - inti =0; - while(++i) the { -EnterCriticalSection (&CS1);Wuyimemset (str,' /',1024x768); thesprintf (str,"Boy %d Times said: I love you Girl", i); -LeaveCriticalSection (&CS1); Wu - //Send event 0 AboutSetEvent (Event[0]); $ - //wait for the intermediary to send the event 3 to execute again -WaitForSingleObject (Event[3], INFINITE); -ResetEvent (Event[3]); A +Sleep ( +); the } - return 0; $ } the theDWORD WINAPI Girl (void*p) the { the inti =0; - while(++i) in { the //events waiting for an intermediary to send 1 theWaitForSingleObject (Event[1], INFINITE); AboutResetEvent (Event[1]); the theEnterCriticalSection (&CS1); thememset (str,' /',1024x768); +sprintf (str,"girl%d Times said: I love you boy", i); -LeaveCriticalSection (&CS1); the Bayi the //Send event 2 theSetEvent (Event[2]); - -Sleep ( +); the } the return 0; the } the - voidMain () the { theInitializeCriticalSection (&CS1); the Event[0] =CreateEvent (null, TRUE, FALSE, null);94 Event[1] =CreateEvent (null, TRUE, FALSE, null); the Event[2] =CreateEvent (null, TRUE, FALSE, null); the Event[3] =CreateEvent (null, TRUE, FALSE, null); the 98hthread[0] = CreateThread (NULL,0, Boy, NULL,0, NULL); Abouththread[1] = CreateThread (NULL,0, Girl, NULL,0, NULL); -hthread[2] = CreateThread (NULL,0, Zhong, NULL,0, NULL);101 102 103WaitForMultipleObjects (2, Hthread, TRUE, INFINITE);104}

20. Event Communication Implementation process repeater mode call

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.