WaitForMultipleObjects에서 두 번째 인자에 뮤텍스 핸들을 전달할 수 없는건가요?

mydream의 이미지

#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이 호출되면 나머지 스레들의 작업을 마치고 작업에 들어가는 것이 아니라 중간에 간섭하며 합계를 구하기 때문에 원래의 성적총합보다 작은 값이 산출됩니다. 뮤텍스 핸들을 전달할 수 있게 하면서 작업을 할 수 없는 건가요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.