views:

318

answers:

5

I have a simple program that creates a thread, loops twenty times and then makes a call to close itself and perform the necessary cleanup.

When I debug the program it reaches the ExitThread(); method and pauses, ignoring the printf(); I have set up after it to signal to me it's closed.

Is this normal or am I forgetting to do something? I'm new to threading using C.

Main()

void main()
{
    Time t;
    int i = 0;

    StartTimer();

    for(i = 0; i < 20; i++)
    {
        t = GetTime();
        printf("%d.%.3d\n", t.seconds, t.milliseconds);
        Sleep(100);
    }
    StopTimer();
}

Thread Creation

void StartTimer()
{
    DWORD threadId;
seconds = 0;
milliseconds = 0;

// Create child thread
hThread = CreateThread(
    NULL,       // lpThreadAttributes (default)
    0,          // dwStackSize (default)
    ThreadFunc, // lpStartAddress
    NULL,       // lpParameter
    0,          // dwCreationFlags
    &threadId   // lpThreadId (returned by function)
    );

// Check child thread was created successfully
if(hThread == NULL)
{
    printf("Error creating thread\n");
}
}

Thread Close

void StopTimer()
{
    DWORD exitCode;

    if(GetExitCodeThread(hThread,&exitCode) != 0)
    {
        ExitThread(exitCode);   
        printf("Thread closed");

        if(CloseHandle(hThread))
        {
            printf("Handle closed");
        }
    }
}

Edit:

I've realised that ExitThread() stops the executing thread, therefore I have attempted to use a different method to stop the timer but i'm still left with the same result.

void StopTimer()
{    
    WaitForSingleObject(hThread,INFINITE);
}

Solution:

The reason the program was stopping at the WaitForSingleObject() call and not progressing no matter how long it took was because the operation it was waiting on had an infinite loop.

It would be waiting for an infinite loop to finish, not the best thing to do. Changing the delay of WaitForSingleObject() to something more reasonable, such as 100 allowed the program to run normally.

Thanks for all the input.

+1  A: 

It is normal, because ExitThread actually exit the current thread, so it will never return and the following instructions will never be executed.

By the way, you should never call directly ExitThread (unless you really know what are you doing): you just have to return from the thread function.

Then, if you use the C runtime library, you should not use the raw thread API, but the C runtime library functions like _beginthread and _beginthreadex.

Lorenzo
Thank you for the clarification
Jamie Keeling
+1  A: 

The ExittThread function terminates the CALLING thread -- immediately.

Giuseppe Guerrini
Thanks for the help
Jamie Keeling
+2  A: 

Yes this is "normal", by calling ExitThread, you're telling to windows you're done with that calling thread, so the rest of the code isn't going to get called. It's a bit like calling exit(0).

Personally I wouldn't use it like this. The rest of the code is OK, you're properly cleaning up the thread, just exit your thread function normally.

Jim Crafton
Thank you for your answer
Jamie Keeling
+1  A: 

Since you are calling StopTimer in the context of the main thread, when it calls exit thread, it ends the main thread and along with it it ends the process.

Jay
Thank you for the assistance Jay
Jamie Keeling
A: 

You can create an Event at the beginning of ThreadFunc, such as hStop, and wait for this event for no time at each loop. then you can use SetEvent() in the main thread to ask the thread to stop.

Note For an executable file linked with LIBCMT.LIB, do not call the Win32 ExitThread API; this prevents the run-time system from reclaiming allocated resources. _endthread and_endthreadex reclaim allocated thread resources and then call ExitThread.

Allopen