WaitForMultipleObjects에서 두 번째 인자에 뮤텍스 핸들을 전달할 수 없는건가요?
글쓴이: mydream / 작성시간: 목, 2016/05/19 - 5:30오후
#include <Windows.h>
#include "PROCESSTHREAD.h"
#include <stdio.h>
struct Student {
const char* name;
unsigned short nation_language;
unsigned short english;
unsigned short mathemetics;
unsigned short society;
unsigned short science;
unsigned short personal_total;
};
Student onegrade[]={ {"김을용", 80, 80, 60, 90, 75, 0}, {"이주용", 100, 80, 96, 60, 88, 0},
{"정오성", 80, 80, 90, 100, 85, 0}, {"홍진경", 85, 85, 70, 90, 84, 0},{"김을용", 80, 80, 60, 90, 75, 0}, {"이주용", 100, 80, 96, 60, 88, 0},
{"정오성", 80, 80, 90, 100, 85, 0}, {"홍진경", 85, 85, 70, 90, 84, 0},{"김을용", 80, 80, 60, 90, 75, 0}, {"이주용", 100, 80, 96, 60, 88, 0},
{"정오성", 80, 80, 90, 100, 85, 0}, {"홍진경", 85, 85, 70, 90, 84, 0},{"김을용", 80, 80, 60, 90, 75, 0}, {"이주용", 100, 80, 96, 60, 88, 0},
{"정오성", 80, 80, 90, 100, 85, 0}, {"홍진경", 85, 85, 70, 90, 84, 0}, {NULL, 0, 0, 0, 0, 0, 0}
};
Student twograde[]={ {"성동일", 70, 75, 88, 90, 100, 0}, {"김하인", 60, 90, 80, 75, 65, 0}, {"정동구", 100, 80, 70, 80, 90, 0},
{"이미진", 80, 90, 70, 70, 95, 0}, {"김구라", 80, 95, 85, 90, 100, 0}, {"정동진", 95, 100, 85, 90, 100, 0},
{"이진욱", 95, 100, 80, 80, 100, 0}, {"성시경", 100, 90, 70, 90, 70, 0}, {"김원반", 100, 80, 90, 80, 70, 0},
{NULL, 0, 0, 0, 0, 0, 0}
};
HANDLE nathandle, enghandle, mathhandle, sochandle, scihandle;
HANDLE totalhandle;
//HANDLE nathandle2, enghandle2, mathhandle2, sochandle2, scihandle2;
HANDLE mutexhandle;
HANDLE mutexhandle2;
DWORD WINAPI natThread(_In_ LPVOID lpParameter);
DWORD WINAPI engThread(_In_ LPVOID lpParameter);
DWORD WINAPI mathThread(_In_ LPVOID lpParameter);
DWORD WINAPI socThread(_In_ LPVOID lpParameter);
DWORD WINAPI sciThread(_In_ LPVOID lpParameter);
DWORD WINAPI calTotalThread(_In_ LPVOID lpParameter);
int main() {
SECURITY_ATTRIBUTES sec;
sec.nLength=sizeof(sec);
sec.lpSecurityDescriptor=NULL;
sec.bInheritHandle=TRUE;
//unsigned int nattotal, engtotal, mathtotal, soctotal, scitotal;
//nattotal=engtotal=mathtotal=soctotal=scitotal=0;
unsigned int total=0;
mutexhandle=CreateMutex(NULL, FALSE, NULL);
mutexhandle2=CreateMutex(NULL, FALSE, NULL);
while( (nathandle=CreateThread(&sec, 0, natThread, NULL, CREATE_SUSPENDED, NULL))==NULL ) {}
while( (enghandle=CreateThread(&sec, 0, engThread, NULL, CREATE_SUSPENDED, NULL))==NULL ) {}
while( (mathhandle=CreateThread(&sec, 0, mathThread, NULL, CREATE_SUSPENDED, NULL))==NULL ) {}
while( (sochandle=CreateThread(&sec, 0, socThread, NULL, CREATE_SUSPENDED, NULL))==NULL ) {}
while( (scihandle=CreateThread(&sec, 0, sciThread, NULL, CREATE_SUSPENDED, NULL))==NULL ) {}
while( (totalhandle=CreateThread(&sec, 0, calTotalThread, &total, CREATE_SUSPENDED, NULL))==NULL ) {}
while( ResumeThread(nathandle)>1 ) {}
while( ResumeThread(enghandle)>1 ) {}
while( ResumeThread(mathhandle)>1 ) {}
while( ResumeThread(sochandle)>1 ) {}
while( ResumeThread(scihandle)>1 ) {}
while( ResumeThread(totalhandle)>1 ) {}
DWORD exitcode1, exitcode2, exitcode3, exitcode4, exitcode5, exitcode6;
WaitForThread(nathandle, &exitcode1);
WaitForThread(enghandle, &exitcode2);
WaitForThread(mathhandle, &exitcode3);
WaitForThread(sochandle, &exitcode4);
WaitForThread(scihandle, &exitcode5);
WaitForThread(totalhandle, &exitcode6);
//nattotal, engtotal, mathtotal, soctotal, scitotal;
//printf("%u %u %u %u %u\n", nattotal, engtotal, mathtotal, soctotal, scitotal);
for(size_t i=0;onegrade[i].name!=NULL;++i) {
printf("%s %u\n", onegrade[i].name, onegrade[i].personal_total);
}
printf("\n\n\n");
for(size_t i=0;twograde[i].name!=NULL;++i) {
printf("%s %u\n", twograde[i].name, twograde[i].personal_total);
}
printf("총점은 %u\n", total);
CloseHandle(nathandle);
CloseHandle(enghandle);
CloseHandle(mathhandle);
CloseHandle(sochandle);
CloseHandle(scihandle);
CloseHandle(totalhandle);
CloseHandle(mutexhandle);
CloseHandle(mutexhandle2);
return 0;
}
DWORD WINAPI natThread(_In_ LPVOID lpParameter) {
for(int i=0;onegrade[i].name!=NULL;++i) {
WaitForSingleObject(mutexhandle, INFINITE);
onegrade[i].personal_total+=onegrade[i].nation_language;
printf("natThread, onegrade.\n");
ReleaseMutex(mutexhandle);
}
for(int i=0;twograde[i].name!=NULL;++i) {
WaitForSingleObject(mutexhandle2, INFINITE);
twograde[i].personal_total+=twograde[i].nation_language;
printf("natTread. twograde.\n");
ReleaseMutex(mutexhandle2);
}
printf("natThread ends.\n");
return 0;
}
DWORD WINAPI engThread(_In_ LPVOID lpParameter) {
for(int i=0;onegrade[i].name!=NULL;++i) {
printf("engThread. onegrade\n");
WaitForSingleObject(mutexhandle, INFINITE);
onegrade[i].personal_total+=onegrade[i].english;
ReleaseMutex(mutexhandle);
}
for(int i=0;twograde[i].name!=NULL;++i) {
printf("engTread. twograde.\n");
WaitForSingleObject(mutexhandle2, INFINITE);
twograde[i].personal_total+=twograde[i].english;
ReleaseMutex(mutexhandle2);
}
printf("engThread ends\n");
return 0;
}
/*unsigned short nation_language;
unsigned short english;
unsigned short mathemetics;
unsigned short society;
unsigned short science;*/
DWORD WINAPI mathThread(_In_ LPVOID lpParameter) {
for(int i=0;onegrade[i].name!=NULL;++i) {
printf("mathThread. onegrade.\n");
WaitForSingleObject(mutexhandle, INFINITE);
onegrade[i].personal_total+=onegrade[i].mathemetics;
ReleaseMutex(mutexhandle);
}
for(int i=0;twograde[i].name!=NULL;++i) {
printf("mathThread. twograde.\n");
WaitForSingleObject(mutexhandle2, INFINITE);
twograde[i].personal_total+=twograde[i].mathemetics;
ReleaseMutex(mutexhandle2);
}
printf("mathThread ends\n");
return 0;
}
DWORD WINAPI socThread(_In_ LPVOID lpParameter) {
for(int i=0;onegrade[i].name!=NULL;++i) {
printf("socThread. onegrade.\n");
WaitForSingleObject(mutexhandle, INFINITE);
onegrade[i].personal_total+=onegrade[i].society;
ReleaseMutex(mutexhandle);
}
for(int i=0;twograde[i].name!=NULL;++i) {
printf("socthread. twograde.\n");
WaitForSingleObject(mutexhandle2, INFINITE);
twograde[i].personal_total+=twograde[i].society;
ReleaseMutex(mutexhandle2);
}
printf("socThread ends.\n");
return 0;
}
DWORD WINAPI sciThread(_In_ LPVOID lpParameter) {
for(int i=0;onegrade[i].name!=NULL;++i) {
printf("sciThread. onegrade.\n");
WaitForSingleObject(mutexhandle, INFINITE);
onegrade[i].personal_total+=onegrade[i].science;
ReleaseMutex(mutexhandle);
}
for(int i=0;twograde[i].name!=NULL;++i) {
printf("sciThread. twograde.\n");
WaitForSingleObject(mutexhandle2, INFINITE);
twograde[i].personal_total+=twograde[i].science;
ReleaseMutex(mutexhandle2);
}
printf("sciThread ends.\n");
return 0;
}
DWORD WINAPI calTotalThread(_In_ LPVOID lpParameter) {
printf("calTotal is beginning.\n");
//HANDLE allhandle[2]={mutexhandle, mutexhandle2};
//WaitForMultipleObjects(2, allhandle, TRUE, INFINITE);
HANDLE allhandle[5]={nathandle, enghandle, mathhandle, sochandle, scihandle};
WaitForMultipleObjects(5, allhandle, TRUE, INFINITE);
for(int i=0;onegrade[i].name!=NULL;++i) {
*((unsigned int*)lpParameter)+=onegrade[i].personal_total;
}
for(int i=0;twograde[i].name!=NULL;++i) {
*((unsigned int*)lpParameter)+=twograde[i].personal_total;
}
printf("calTotal is ending.\n");
return 0;
}calTotalThread에서는 최종적으로 성적의 총합을 구합니다. 그래서 나머지 스레드들이 개인별 성적 총합을 각각 다 구하고 나면 실행이 되야 하거든요. 그 스레드들도 뮤텍스 핸들로 두 개의 락을 씁니다. calTotalThread 함수 안에서 WaitForMultipleObjects엣 두 번째 인자에 mutexhandle, mutexhandle2를 값으로 갖는 배열을 전달하려 하는데 그렇게 하니 마지막에 WaitForMultipleObjects가 작업을 시작하지 않네요.
HANDLE mutexhandles[2]={mutexhandle, mutexhandle2};
WaitForMultipleObjects(2, mutexhandles, TRUE, INFINITE);이렇게 일부를 수정하고 calTotalThread이 호출되면 나머지 스레들의 작업을 마치고 작업에 들어가는 것이 아니라 중간에 간섭하며 합계를 구하기 때문에 원래의 성적총합보다 작은 값이 산출됩니다. 뮤텍스 핸들을 전달할 수 있게 하면서 작업을 할 수 없는 건가요?
Forums:


댓글 달기