/*
* Numbers.c
*
* Sample code for "Multithreading Applications in Win32"
* This is from Chapter 2, Listing 2-1
*
* Starts five threads and gives visible feedback
* of these threads running by printing a number
* passed in from the primary thread.
*
*/
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
DWORD WINAPI ThreadFunc(LPVOID);
int main()
{
HANDLE hThrd;
DWORD threadId;
int i;
for (i=0; i<5; i++)
{
hThrd = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)i,
0,
&threadId );
if (hThrd)
{
printf("Thread launched %d\n", i);
CloseHandle(hThrd);
}
}
// Wait for the threads to complete.
// We'll see a better way of doing this later.
Sleep(2000);
printf ("Enter any key for terminating...\n");
_getch ();
return EXIT_SUCCESS;
}
DWORD WINAPI ThreadFunc(LPVOID n)
{
int i;
for (i=0;i<10;i++)
printf("%d%d%d%d%d%d%d%d\n",n,n,n,n,n,n,n,n);
return 0;
}
일반적으로 함수 호출과 달리 순서가 뒤죽박죽이다.
이 순서를 앞으로 조절해가는 것이 목표이다.
=== 결과 ===
Thread launched 0
00000000
Thread launched 1
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Thread launched 2
00000000
00000000
00000000
Thread launched 3
11111111
11111111
Thread launched 4
44444444
33333333
33333333
33333333
33333333
33333333
33333333
33333333
33333333
33333333
33333333
44444444
44444444
44444444
44444444
44444444
44444444
44444444
44444444
44444444
22222222
22222222
22222222
22222222
22222222
22222222
22222222
22222222
22222222
22222222
00000000
00000000
00000000
00000000
00000000
00000000
Enter any key for terminating...
'Programming > Thread in Win32' 카테고리의 다른 글
ExitThread (0) | 2017.01.05 |
---|---|
ExitCode (0) | 2017.01.05 |