简体   繁体   中英

Couldn't terminate thread (error 6)

We have a huge, complex wxWidgets application written in C++. I added an extra background thread. When the user clicks "go", the thread starts. When they click "stop", the thread stops. For reasons beyond my comprehension, clicking "stop" also causes the following message to be displayed:

Can not wait for thread termination (error 6: the handle is invalid.)
Couldn't terminate thread (error 6: the handle is invalid.)

Why the hell is this happening?? And more importantly, how do I make this go away immediately?

The thread is started here:

_worker = new WorkerThread();
_worker->Create();
_worker->Run();

I know for a fact that the thread is running, because I can see the disk files it's writing.

The thread is stopped here:

if (_worker)
{
  _worker->Delete();
  _worker = NULL;
}

The WorkerThread class only overrides Enter() . It is definitely a detachable thread.

The documentation is full of dire warnings about how a detachable thread can delete itself at any moment, and everything must always be wrapped in a critical section. But my worker thread runs forever, until I tell it to stop. I can't see why I would need a critical section for anything.

Is the thread taking too long to stop? Is that the problem? (It only checks TestDestroy() once per second. Is that too slow?)

I really can't figure out how the hell to solve this.

You may "make it go away" by using wxLogNull , as with any other messages generated by wxWidgets. You should not do this however as you seem to have a real bug somewhere in your code, the thread handle obviously should not be invalid and if it is, something clearly doesn't go as you think it does. By sweeping the error under the carpet you all but guarantee that it will reappear in a different guise at the worst possible moment and typically on a clients machine where you will be unable to debug it. Better really do it now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM