TerminateThread ???

In MSDN it is mentioned that TerminateThread is dangerous function and given some disadvantages of using it.

So we should never use TerminateThread If so then what is the use of this function

Consider a situation where I am calling a function from third party library and that function some time takes long time to return may be 5 hours or may be more than a day (I am not sure about the reason and that is not in my control). I need to implement some timeout mechanism so I called that function in a thread and waiting for 15 minutes to return it and then terminating the thread and proceeding further. The thread is just calling that function doing nothing else.

So is it ok if I call TerminateThread to terminate that thread after timeout

Or is it really dangerous

If so how can I implement timeout mechanism for a function which I don’t have any control.

Please help and give suggestions.

Thanks,

Amol




Answer this question

TerminateThread ???

  • x868

    on the surface TerminateThread is comparable to function return. theres always some reasons, so one example would be an infinite loop. never even thought of TerminateThread

    Kuphryn

  • hommer

    If a call into your 3rd party library hangs for that long, it's a problem the 3rd party library needs to solve, and I personally wouldn't want to use that sort of unpredictable library on any of my apps.

    But if you are forced to use a 3rd party library, and a function call occasionally freezes, then you'd be forced to call TerminateThread on the thread that invoked the function all.

    But again, you really need to contact their support and find out what's wrong.



  • Vince P

    The problem with TerminateThread is that there is no clean-up done. You'll leak resources up to wazoo. None of the kernel handles will get closed, heap blocks don't get deallocated. Worst: you'll leak the 1 megabyte virtual memory space reserved for the thread's stack. If the thread is truly hung and not responding to quit requests, you're much better off just terminating your program...


  • arogan

    The reason for it being dangerous is simply that whatever is going on is not allowed to terminate gracefully. Resources opened or allocated are not closed or freed. Handles to OS internals may be left dangling, and so on.

    The need for TerminateThread is more often than not the result of poor or lacking design. In your case, whatever mechanism you are contacting should / could let you specify how long you want to wait at max. Another option is to start the operation, and asynchronously wait for it to complete, e.g. by having it instantly return an event handle which could be checked at certain intervals. Upon completion, you could call another function, supply the event handle, and e.g. get resulting data in return. A third function would be provided for breaking an operation which has gone on for too long, thus eliminating the need to terminate it the hard way.

    Telling whether or not it is actually dangerous to use TerminateThread in your case, is pretty much impossible without knowledge of what this remote mechanism really does. The very same goes for design suggestions.

    So, in conclusion: Yes, it can be dangerous, yet it may also be ok (and even perfectly harmless) for you to do this.



  • TerminateThread ???