Question

I have a ThreadA created using the CreateThread function. Inside ThreadA, another thread, ThreadB is created using the CreateThread function.

Now, I want to terminate ThreadB from ThreadA but the TerminateThread function fails.

Is it because ThreadA may not have the right to terminate ThreadB? I am really confused with this situation.

Was it helpful?

Solution

In general, never use TerminateThread because you can leave locks held and cause all sorts of problems. As MSDN says:

MSDN

What you can do instead is to make your thread wait on an event object, which you can use to signal termination from another thread.

If you need to perform a periodic check for a terminate signal within some normal thread processing, you can set a wait timeout of zero so your thread can do still work while waiting to be terminated.

An example is in this SO answer here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top