- 
          
          MutexOperations Dev/writech 2010. 12. 10. 18:24
 // Example02_Mutex.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
 //#include "stdafx.h" #include "Windows.h" 
 #include <process.h>//#include <concrt.h> #include <string> 
 #include <iostream>
 using namespace std;unsigned int WINAPI writeCharacter( LPVOID param); string g_string; 
 int const MAX_THREAD = 5;
 HANDLE g_mutexHandle;int _tmain(int argc, _TCHAR* argv[]) 
 {
 //가용 공간을 미리 예약해 놓는 부분,
 //g_string.reserve( MAX_THREAD * 1000 +1 );g_mutexHandle = ::CreateMutexA( NULL, FALSE, NULL); //char ch = 'a'; 
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];for(int i=0; i < MAX_THREAD; ++i) 
 {
 alphas[i] = 'a' + i;
 threadHandles[i] = (HANDLE)::_beginthreadex( NULL,
 0,
 writeCharacter,
 (LPVOID*)(&alphas[i]), // LPVOID 포인터 타입 무슨 타입인지알아야한다. 이것은 char타입니다.
 0,
 (unsigned int *)&threadIDs[i]);} DWORD result = 
 ::WaitForMultipleObjects(MAX_THREAD,
 &threadHandles[0],
 TRUE,
 INFINITE);//결과값이 제대로 나온다면 
 if(result == WAIT_OBJECT_0)
 {
 cout<<g_string;
 }//::ReleaseMutex(g_mutexHandle); for(int i=0; i < MAX_THREAD; ++i) 
 {
 CloseHandle(threadHandles[i]);
 }
 CloseHandle( g_mutexHandle);
 return 0;
 }unsigned int WINAPI writeCharacter(LPVOID param) 
 {
 char ch = *((char*)param);DWORD result = ::WaitForSingleObject(g_mutexHandle,INFINITE); // 대기시간 무한! for(int i = 0; i < 1000; ++i) 
 {
 g_string += ch;
 }::ReleaseMutex(g_mutexHandle); return 0; 
 }'Operations Dev > writech' 카테고리의 다른 글Semaphore (0) 2010.12.10 CriticalSection (0) 2010.12.10 SpinLock (0) 2010.12.10 SpinLock2 (0) 2010.12.10 SimpleConcurrency (0) 2010.12.10