[ 전역변수를 이용 ]
#include "stdafx.h"
#include <windows.H>
#include <stdio.H>
#include <tchar.H>
volatile int data1;
volatile int data2;
DWORD CALLBACK TestThread1(void* arg)
{
for (int i = 0; i < 500000000; ++i)
data1 = data1 + 1;
return data1;
}
DWORD CALLBACK TestThread2(void* arg)
{
for (int i = 0; i < 500000000; ++i)
data2 = data2 + 1;
return data2;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE thread[2];
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
DWORD startTime = GetTickCount();
thread[0] = CreateThread(NULL, 0, TestThread1, (LPVOID)0, 0, NULL);
thread[1] = CreateThread(NULL, 0, TestThread2, (LPVOID)0, 0, NULL);
WaitForMultipleObjects(2, thread, TRUE, INFINITE);
_tprintf(_T("%d\n"), GetTickCount() - startTime);
return 0;
}
[ 지역변수를 이용 ]
#include "stdafx.h"
#include <windows.H>
#include <stdio.H>
#include <tchar.H>
//volatile int data1;
//volatile int data2;
DWORD CALLBACK TestThread1(void* arg)
{
int data1;
for (int i = 0; i < 500000000; ++i)
data1 = data1 + 1;
return data1;
}
DWORD CALLBACK TestThread2(void* arg)
{
int data2;
for (int i = 0; i < 500000000; ++i)
data2 = data2 + 1;
return data2;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE thread[2];
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
DWORD startTime = GetTickCount();
thread[0] = CreateThread(NULL, 0, TestThread1, (LPVOID)0, 0, NULL);
thread[1] = CreateThread(NULL, 0, TestThread2, (LPVOID)0, 0, NULL);
WaitForMultipleObjects(2, thread, TRUE, INFINITE);
_tprintf(_T("%d\n"), GetTickCount() - startTime);
return 0;
}