Thread 2: Thread communication event mechanism (single-threaded, two-thread, multiple-thread communication)

Source: Internet
Author: User
Tags sprintf

Communication between a thread DWORD WINAPI firstthread (void *p) {messageboxa (0, "1", "1", 0); printf ("1th thread execution complete \ n"); SetEvent (event[0]);//Send event signal return 0; } DWORD WINAPI secondthread (void *p) {WaitForSingleObject (event[0], INFINITE);//wait for the event signal to appear, perform the next messageboxa (0, "2", " 2 ", 0); printf ("2nd thread execution complete \ n"); return 0; } void Main () {//security_attributes, specifies a structure that sets the security characteristics of the object. If it becomes ByVal as Long and passes a value of 0, it indicates that the default security setting of the object is used//long, if true, to create a manual reset event, or false to create an automatic reset event//long If the event should enter the trigger state internally; true String that specifies the name of the event object. Creates an unnamed event object with vbNullString. If an event with this name already exists,//Then the existing named event will open. The name may not match the name of an existing mutex, signal machine, waiting timer, or file mapping event[0] = CreateEvent (null, TRUE, FALSE, NULL);//create event Event[1] = CreateEvent (NULL , TRUE, FALSE, NULL);  Hthread[0]=createthread (null, 0, firstthread, NULL, 0, NULL); HTHREAD[1] = CreateThread (null, 0, secondthread, NULL, 0, NULL);  WaitForMultipleObjects (2, Hthread, TRUE, INFINITE); printf ("Thread task complete \ n"); System ("pause"); }


Communication between two threads DWORD WINAPI AAA (void *p) { int i = 1;printf ("AAA%d times: Signaling aaaaa......\n", i); SetEvent (event[0]);//Here as a first-time speaker  Otherwise it will cause a deadlock while (++i) {WaitForSingleObject (event[1], INFINITE);p rintf ("AAA% D said: Signal aaaaa......\n ", i); SetEvent (Event[0]); Sleep (1000);}} DWORD WINAPI bbb (void *p) {int i = 0;while (++i) {WaitForSingleObject (event[0], INFINITE);p rintf ("BBB%d" said: Signal bbbbb ... \ n ", i); SetEvent (event[1]); Sleep (1000);}} void Main () {event[0] = CreateEvent (null, True, FALSE, null), event[1] = CreateEvent (null, True, FALSE, null); Hthread[0] = C Reatethread (null, 0, AAA, NULL, 0, NULL); Hthread[1] = CreateThread (null, 0, BBB, NULL, 0, NULL); WaitForMultipleObjects (2, Hthread, TRUE, INFINITE); System ("Pause");}


HANDLE Event[4] = {0};//event HANDLE hthread[3] = {0};//represents thread critical_section Cs;char str[1024] = {0};//buffer for chat content DWORD W INAPI Zhouruifu (void *p) {int i = 0;int k = 0;while (++i) {printf ("--------------------\ n"), if (k==0) {printf ("------------ --------k=0\n "); WaitForSingleObject (Event[0], INFINITE); EnterCriticalSection (&cs);p rintf ("\ n Mediation:%d%s", I, str); LeaveCriticalSection (&CS); Sleep (1000); SetEvent (event[1]);//Send to 1k = 1;} else {printf ("--------------------k=1\n"); WaitForSingleObject (event[2], INFINITE);//wait 2EnterCriticalSection (&CS);p rintf ("\ n Pass data:%d%s", I, str); LeaveCriticalSection (&CS); Sleep (1000); SetEvent (event[3]);//Send to 3k = 0;}}} DWORD WINAPI Hello (void *p) {int i = 1; EnterCriticalSection (&cs); memset (str, ' \nhello ', 1024x768); sprintf (str), "the first%d Times said: I love your World", I); LeaveCriticalSection (&CS); Sleep (1000); SetEvent (Event[0]); while (++i) {WaitForSingleObject (event[3], INFINITE); EnterCriticalSection (&cs); memset (str, ' \nhello ', 1024);//Clear Contents sprintf (str, "the".%d): I loVE world ", I); LeaveCriticalSection (&CS); Sleep (1000); SetEvent (Event[0]);} return 0;} DWORD WINAPI World (void *p) {int i = 0;while (++i) {WaitForSingleObject (event[1], INFINITE); EnterCriticalSection (&cs); memset (str, ' \nworld ', 1024x768); sprintf (str), "The first%d said: I love you Too", I); LeaveCriticalSection (&CS); Sleep (1000); SetEvent (event[2]);} return 0;} Communication Mediator pattern void main () between three threads InitializeCriticalSection (&CS);//Initialize event[0] = CreateEvent (null, TRUE, FALSE, NULL);//create event Event[1] = CreateEvent ( NULL, True, FALSE, null); event[2] = CreateEvent (null, True, FALSE, null); Event[3] = CreateEvent (null, True, FALSE, NULL); h Thread[0] = CreateThread (null, 0, hello, null, 0, NULL); Hthread[1] = CreateThread (null, 0, world, NULL, 0, NULL); hthread[2 ] = CreateThread (null, 0,ZHOURUIFU, NULL, 0, NULL); WaitForMultipleObjects (2, Hthread, TRUE, INFINITE);p rintf ("over");D eletecriticalsection (&CS); System ("pause");

Copyright NOTICE: This article is for bloggers original article, welcome to point out the code is bad, and put forward the Code optimization scheme. Welcome guidance, Night code, desperately struggling to update in ...

Thread 2: Thread communication event mechanism (single-threaded, two-thread, multiple-thread communication)

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.