user1944429
user1944429

Reputation:

TerminateThread locks up when thread has infinate loop

So I have been learning about threads in the Windows API, and I wrote some test code, but I am have some issues. First off I have two functions:

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{
    int i = (int)lpParam;
    cout << i << ".) I work! " << endl;
    return 0;
}

And

// MyThreadFunction2 - loops continuously forever, outputing "working..." every 1ms
DWORD WINAPI MyThreadFunction2( LPVOID lpParam ) 
{
        int i = (int)lpParam;
    while(1) {
        cout << i << ".) Working... " << endl;
        Sleep(1);
    }
    return 0;
}

And I am creating threads using CreateThread, but I will change that later to _beginThread which is what people have told me to do in a different but related post. So this is how I have been creating threads:

    threads.push_back(CreateThread(
                                   NULL,           // default security attributes
                                   0,              // use default stack size
                                   lpStartAddress, // thread function name
                                   lpParameter,    // arugument to thread function
                                   0,              // use default creation flag
                                   NULL));         // ignore thread identifier.

I am putting the thread HANDLEs I create into a vector, but that is irrelevant. Basically I have the above code working, and it creates the threads fine. The problem I am having is calling TerminateThread(HANDLE, 0) when the thread was created using MyThreadFunction2, where it loops forever. When I call TerminateThread, my program just freezes and sits there. It appears the thread has stopped, because I don't get the "Working..." message, but it doesn't continue any other code. However, when I call this same function when it is with MyThreadFunction, it works fine.

I don't know if this is extremely confusing how I am explaining what is going on, I hope it isn't. I know this isn't a place to "just dump code, and have people fix it" - I read that on someone else's post, but just incase you didn't understand a thing I wrote above, or it is helpful, here is my code:

ThreadManager.cpp

http://pastebin.com/0B1N3TAH

ThreadManager.h

http://pastebin.com/yfwMJTaz

Sorry the code is poorly written, and some things aren't complete. I am still working on re-learning C++ and learning how to use the Windows API. But does anyone have any ideas of what I might possibly be doing wrong?

Upvotes: 0

Views: 1721

Answers (1)

sdkljhdf hda
sdkljhdf hda

Reputation: 1407

Using TerminateThread to kill a thread is generally a really bad idea. Even MSDN page for this function tells you that. A better way to tell the threads to stop working is to pass them an event. With an event the thread that runs an infinite loop would do while (WaitForSingleObject(event, 1) == WAIT_TIMEOUT) instead of while (1).

Why exactly TerminateThread does what it dies, I don't know, but you can get an idea of what might be wrong when you read this http://msdn.microsoft.com/en-us/library/windows/desktop/ms686717(v=vs.85).aspx

Upvotes: 4

Related Questions