Topic:
Four threads T1,t2,t3,t4, write data to 4 files, T1 can only write 1, T2 can only write 2, T3 can only write
In 3, T4 can only write 4, 4 files A, B, C, D write the following content
A:123412341234 .....
b:234123412341 ....
c:341234123412 ....
D:412341234123 ....
How can synchronization be implemented to allow threads to work in parallel?
Implemented in Windows C + +.
//222_thread.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<windows.h>#include<thread>#include<iostream>using namespacestd; HANDLE Ghmutex; HANDLE handles[4][4];intIdxx =1;voidThreada () {intIDX =0; Do{DWORD res= WaitForMultipleObjects (4, handles[0],false, INFINITE); WaitForSingleObject (Ghmutex, INFINITE); Switch(res) { CaseWait_object_0:cout<<"A" ; SetEvent (handles[1][0]); Break; Casewait_object_0+1: cout<<"B"; SetEvent (handles[1][1]); Break; Casewait_object_0+2: cout<<"C"; SetEvent (handles[1][2]); Break; Casewait_object_0+3: cout<<"D"; SetEvent (handles[1][3]); Break; } cout<<1; Idxx++; ReleaseMutex (Ghmutex); } while(Idxx < -); }voidthreadb () {intIDX =0; Do{DWORD res= WaitForMultipleObjects (4, handles[1],false, INFINITE); WaitForSingleObject (Ghmutex, INFINITE); Switch(res) { CaseWait_object_0:cout<<"A"; SetEvent (handles[2][0]); Break; CaseWait_object_0 +1: cout<<"B"; SetEvent (handles[2][1]); Break; CaseWait_object_0 +2: cout<<"C"; SetEvent (handles[2][2]); Break; CaseWait_object_0 +3: cout<<"D"; SetEvent (handles[2][3]); Break; } cout<<2; Idxx++; ReleaseMutex (Ghmutex); } while(Idxx < -);}voidTHREADC () {intIDX =0; Do{DWORD res= WaitForMultipleObjects (4, handles[2],false, INFINITE); WaitForSingleObject (Ghmutex, INFINITE); Switch(res) { CaseWait_object_0:cout<<"A"; SetEvent (handles[3][0]); Break; CaseWait_object_0 +1: cout<<"B"; SetEvent (handles[3][1]); Break; CaseWait_object_0 +2: cout<<"C"; SetEvent (handles[3][2]); Break; CaseWait_object_0 +3: cout<<"D"; SetEvent (handles[3][3]); Break; } cout<<3; Idxx++; ReleaseMutex (Ghmutex); } while(Idxx < -);}voidThreadd () {intIDX =0; Do{DWORD res= WaitForMultipleObjects (4, handles[3],false, INFINITE); WaitForSingleObject (Ghmutex, INFINITE); Switch(res) { CaseWait_object_0:cout<<"A"; SetEvent (handles[0][0]); Break; CaseWait_object_0 +1: cout<<"B"; SetEvent (handles[0][1]); Break; CaseWait_object_0 +2: cout<<"C"; SetEvent (handles[0][2]); Break; CaseWait_object_0 +3: cout<<"D"; SetEvent (handles[0][3]); Break; } cout<<4; Idxx++; ReleaseMutex (Ghmutex); } while(Idxx < -);}voidWritea () {SetEvent (handles[0][0]);}voidWriteb () {SetEvent (handles[1][1]);}voidWritec () {SetEvent (handles[2][2]);}voidwrited () {SetEvent (handles[3][3]);}int_tmain (intARGC, _tchar*argv[]) { /*file A file B file C file D thread 1 handle[0][0] handle[0][1] handle[0][2] handle[ 0][3] Thread 2 handle[1][0] handle[1][1] handle[1][2] handle[1][3] thread 3 handle[2][0] handle[2][1] handle[2] [2] handle[2][3] Thread 4 handle[3][0] handle[3][1] handle[3][2] handle[3][3]*/ for(inti =0; I <4; i++) { for(intj =0; J <4; J + +) {Handles[i][j]= CreateEvent (NULL,false,false, NULL); }} DWORD ThreadID; CreateThread (NULL,0, (Lpthread_start_routine) Threada, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) threadb, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) THREADC, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) Threadd, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) Writea, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) Writeb, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) Writec, NULL,0, &ThreadID); CreateThread (NULL,0, (Lpthread_start_routine) writed, NULL,0, &ThreadID); Ghmutex=CreateMutex (NULL,//Default security AttributesFALSE,//initially not ownedNULL);//Unnamed Mutex if(Ghmutex = =NULL) {printf ("CreateMutex Error:%d\n", GetLastError ()); return 1; } Sleep (60000); return 0;}
Interview Questions-Multithreaded programming