// Example01_Simple.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include<windows.h>
#include<process.h>
#include<string>
#include<iostream>
using namespace std;
unsigned int WINAPI writeCharacter(LPVOID param);
string g_string;
int const MAX_THREAD = 5;
int _tmain(int argc, _TCHAR* argv[])
{
g_string.reserve(MAX_THREAD * 1000 +1); //가용공간 예약부분
//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]),0,(unsigned int *)&threadIDs[i]);
}
DWORD result =
::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE); //블록상태로 전환시킨다. 이 커널Object가 사용가능할 때까지
//대기시간 무한
if(result == WAIT_OBJECT_0) //대기가 끝난후 결과가 나오면 출력
{
cout<<g_string;
}
for(int i = 0; i< MAX_THREAD; ++i)
{
CloseHandle(threadHandles[i]);
}
return 0;
}
unsigned int WINAPI writeCharacter(LPVOID param)
{
char ch = *((char *)param);
for(int i = 0; i< 1000; ++i)
{
g_string += ch;
}
return 0;
}
'Operations Dev > writech' 카테고리의 다른 글
Mutex (0) | 2010.12.10 |
---|---|
Semaphore (0) | 2010.12.10 |
CriticalSection (0) | 2010.12.10 |
SpinLock (0) | 2010.12.10 |
SpinLock2 (0) | 2010.12.10 |