/*

 * ExitThrd.c

 *

 * Sample code for "Multithreading Applications in Win32"

 * This is from Chapter 2, Listing 2-3

 *

 * Demonstrate ExitThread

 */


#define WIN32_LEAN_AND_MEAN

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <windows.h>


DWORD WINAPI ThreadFunc(LPVOID);

void AnotherFunc(void);


int main()

{

    HANDLE hThrd;

    DWORD exitCode = 0;

    DWORD threadId;

    

    hThrd = CreateThread(NULL,

        0,

        ThreadFunc,

        (LPVOID)1,

        0,

        &threadId );

    if (hThrd)

        printf("Thread launched\n");


    for(;;)

    {

        BOOL rc;

        rc = GetExitCodeThread(hThrd, &exitCode);

        if (rc && exitCode != STILL_ACTIVE )

            break;

    }


    CloseHandle(hThrd);


    printf("Thread returned %d\n", exitCode);


_getch ();


    return EXIT_SUCCESS;

}



/*

 * Call a function to do something that terminates

 * the thread with ExitThread instead of returning.

 */

DWORD WINAPI ThreadFunc(LPVOID n)

{

    printf("Thread running\n");

    AnotherFunc();

    return 0;

}


void AnotherFunc()

{

    printf("About to exit thread\n");

    ExitThread(4);

    // It is impossible to get here, this line

    // will never be printed

    printf("This will never print\n");

}


ExitThread(ExitCode) 으로 thread가 종료된다.
그러므로, 이후 코드는 실행되지 않는다.
ExitCode는 GetExitCodeThread에게 전달된다.
ExitCode가 4로 임을 볼 수 있다.


--- 결과 ---

Thread launched
Thread running
About to exit thread
Thread returned 4



'Programming > Thread in Win32' 카테고리의 다른 글

ExitCode  (0) 2017.01.05
NUMBERS  (0) 2017.01.05
Posted by 루나s
,