Thread TestDestroy() Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
rodrigod
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Thu Jun 26, 2008 8:50 pm

Thread TestDestroy()

Post by rodrigod »

I have a simple doubt, if inside a thread I call TestDestroy(), and Delete has been called outside it, will it imeadiatly after calling testdestroy leave the function it was executing and call OnExit and destroy the thread?

For example my thread calls a funtion that in this function I have a loop that i testdestroy it after everyloop. But instead of simply stopping executing after the test i want to release a mutex and print a message. Will it execute if it comes after the testdestroy. See the code below:

Code: Select all

UINT FooThread(void* pParam)
{
/*Some initializations*/
//...

while(!pCanal->boolTerminate)
{
/*loop*/
  if ( pCanal->pOb->TestDestroy() )
  {
      pCanal->boolTerminate = true;
  }
}

if ((shoLogCanal[pCanal->uchCanal] & 0x0100) == 0x0100)
{
   wxLogDebug("leaving thread");
)			
SetEvent(pCanal->hevTerminate);
}


So I want it to finish executing the function after testdestoy, will it?
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Post by catalin »

You should first read http://docs.wxwidgets.org/trunk/classwx_thread.html
Also see the thread.cpp code sample; specifically MyThread::Entry() or MyWorkerThread::Entry().

Referring to your code, beside the busy waiting (!), I'm sure you can directly use the check for TestDestroy() wherever you check boolTerminate ...

Finally, yes, calling Delete() for a joinable thread will make TestDistroy() for the same thread return true.
rodrigod
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Thu Jun 26, 2008 8:50 pm

Post by rodrigod »

Thanks,

I had read only the htmlhelp that comes with the library and the sample, but on this documentation the it has a better example for what i needed. It solved more than one doubt I had.
Kraymer
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 04, 2009 1:32 pm
Location: Germany
Contact:

delete() vs. wait()

Post by Kraymer »

catalin wrote:Finally, yes, calling Delete() for a joinable thread will make TestDistroy() for the same thread return true.
I have a question about that: When calling Delete(), a Wait() afterwards will fail due to an "invalid handle". That's understandable however and the documentation does warn that this might happen. However, when I call Wait() on a joinable thread, it will enter what appears to be an endless loop. And yes, I'm calling TestDestroy, so I don't understand how it's supposed to work.

As I understand it, calling Wait() should be enough to end a joinable thread but for me, this just isn't the case (MSW, wx 2.8.10). Do you have any suggestions?
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

I suggest some search into this forum. In particular, some discussion i've had about threads and TestDestroy():
http://forums.wxwidgets.org/viewtopic.php?p=98652#98652
http://forums.wxwidgets.org/viewtopic.php?p=98697#98697

In a nutshell:
- IMHO TestDestroy() must only be used in the thread context, and not in the main thread's one (which means, not as you do!)
- TestDestroy() has do be used with Delete(), otherwise it's useless
- boolTerminate is redundant with TestDestroy() and interferes code readability
- TestDestroy() just returns a boolean value and does not do special thread management
- code lines after TestDestroy() will be executed once TestDestroy() returns "false"
- never tested this, but maybe wxThead::OnExit can be overriden to so some final cleaning
Kraymer
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 04, 2009 1:32 pm
Location: Germany
Contact:

Post by Kraymer »

I think what I was just missing in the documentation is the following:
Because you can (of course?) use TestDestroy() in a joinable thread, it makes sense to use Delete() on it. You then of course still have to Wait() for it to finish.
Delete() is never mentioned with deletion of joinable threads, as if it was such a strange scenario to delete it before it completes its task.. And it took me quite some time to figure out that:

thread->Create();
thread->Run();
.. <wait so time / sleep>..
thread->Delete();
thread->Wait();

is perfectly ok. Hm, is it? I never got a clear answer on that. Still it works ok for me for a while now.
Post Reply