Topic:
Write a program that opens3a thread, which3Threads ofIDrespectively forA,B,C, each thread will own theIDPrint on the screenTenthe output must be orderedABCdisplay in the order of the following;ABCABC... ...recursion in turn.
#include <stdio.h> #include <windows.h> #include <iostream> #include <process.h> #include < Time.h>using namespace Std; HANDLE g_a, G_b, G_c; critical_section g_cs;unsigned int __stdcall PrintA (void* pM) {int i = 0;while (i <) {WaitForSingleObject (g_a, INFINIT E); EnterCriticalSection (&g_cs); cout <<i+1<< ": A"; SetEvent (G_b); ++i; LeaveCriticalSection (&g_cs);} return 0;} unsigned int __stdcall printb (void* pM) {int i = 0;while (i <) {WaitForSingleObject (G_b, INFINITE); EnterCriticalSection (&g_cs); cout << ' B '; SetEvent (g_c); ++i; LeaveCriticalSection (&g_cs);} return 0;} unsigned int __stdcall printc (void* pM) {int i = 0;while (i <) {WaitForSingleObject (G_c, INFINITE); EnterCriticalSection (&g_cs); cout << "c\n"; SetEvent (g_a); ++i; LeaveCriticalSection (&g_cs);} return 0;} int _tmain (int argc, _tchar* argv[]) {initializecriticalsection (&g_cs); g_a = CreateEvent (null, FALSE, TRUE, NULL); g_ b = CreateEvent (NULL, FALSE, FalsE, null); G_c = CreateEvent (null, FALSE, FALSE, NULL); HANDLE Threads[3];threads[0] = (HANDLE) _beginthreadex (null, 0, PrintA, NULL, 0, NULL); Threads[1] = (HANDLE) _ Beginthreadex (null, 0, PRINTB, NULL, 0, NULL); threads[2] = (HANDLE) _beginthreadex (null, 0, PRINTC, NULL, 0, NULL); WaitForMultipleObjects (3, threads, TRUE, INFINITE); CloseHandle (G_A); CloseHandle (G_b); CloseHandle (G_c); CloseHandle (Threads[0]); CloseHandle (Threads[1]); CloseHandle (threads[2]);D eletecriticalsection (&g_cs); cout << endl;system ("pause"); return 0;}
Turn on 3 threads to print ABC10 times sequentially