Should you call Wait on a wxThreadHelper joinable thread that has stopped

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
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by silver.moon »

Is it wrong to called Wait on joinable thread inside wxThreadHelper that has ended.

Code: Select all

GetThread()->Wait();
Doing so give this nice error dialog

Image

But the docs of wxThread say -

Code: Select all

Regardless of whether it has terminated or not, you should call Wait() on a joinable thread to release its memory, as outlined in Types of wxThreads
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by evstevemd »

Any reasona you are not using Detached Threads?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by silver.moon »

evstevemd wrote:Any reasona you are not using Detached Threads?
Never thought about it. What are the pros and cons ?
There are situations when i need to manually stop threads, how would I do that with a Detached thread ?
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by DenDev »

Where du you call GetThread()->Wait()? If you do this in the threads own context (in the Entry() function) it will fail (exception) or cause you program to hang since it will wait for itself to finish (indefinite loop). The Wait() function must always be called outside the threads context (by the thread that started it). If you call the Wait() function for a thread that has already finished it will cause problems, this should be avoided by checking "GetThread() != NULL && GetThread()->IsRunning()". The thread helper is used for a single background task, it cannot be started multiple times and the attempt will restart the task in a "dirty" way.

Using a joinable thread is an advantage if the task it does has to be synchronized with a visual interface. This could be a dialog that copies a file, downloads a file or something like that, and displays the status in a progress bar. In every situation where the thread(s) does not need to have a constant, visual representation (but still can have) detached threads are better. This could be a chat server where each detached thread manages a chatting client or an FTP server where each thread serves one connected FTP client. These invisible "servers" are usually called daemons. In such scenarios there would be no need for the server to display anything (waste of energy). If an admin starts the daemons GUI, the status of the detached threads can still be polled into the visual interface.

So; Joinable threads are only good for single background tasks with constant, visual feedback. Detached threads are good for everything :-)
I have a bad habbit of not testing the code I post :D
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by silver.moon »

Code: Select all

If you call the Wait() function for a thread that has already finished it will cause problems
The why does the docs say (http://docs.wxwidgets.org/trunk/classwx_thread.html)

Code: Select all

Regardless of whether it has terminated or not, you should call Wait() on a joinable thread to release its memory, as outlined in Types of wxThreads
--

Code: Select all

 The thread helper is used for a single background task, it cannot be started multiple times and the attempt will restart the task in a "dirty" way.
I am not starting it multiple times. Just reusing it "after it is done". I guess there is nothing wrong with that. Once the thread is finished, it can be started again by pressing the start button.

So should CreateThread be called again ? or the existing thread object that was created in the previous call to CreateThread be used ?

Code: Select all

So; Joinable threads are only good for single background tasks with constant, visual feedback. Detached threads are good for everything
So how do you stop a detached thread "on-demand", when the user presses the "Stop" button.
The thread might already have finished and then calling IsRunning or Delete will cause problems ?
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by DenDev »

1) You should not call the Wait() function if the thread has already been released. Before you call the Wait() function you must check that the wait operation is valid or you might get into trouble.

2) You can of course reuse the thread helper as many times as you require. But you cannot start it (with CreateThread) while it is already running with causing a dirty termination of the thread already running.

3) You must keep a reference to the thread in a variable. The variable should be set to NULL when the thread finishes using an event to signal that the thread has finished. Please view the sample on this page http://docs.wxwidgets.org/trunk/classwx_thread.html the MyFrame::OnClose method shows how to do this.
I have a bad habbit of not testing the code I post :D
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by silver.moon »

Code: Select all

1) You should not call the Wait() function if the thread has already been released. Before you call the Wait() function you must check that the wait operation is valid or you might get into trouble.
Incase of JOINABLE threads inside wxThreadHelper "its released" only in wxThreadHelper destructor. So as long as the wxThreadHelper object is alive calling Wait should be always valid.

But inspite of that I get the error dialog shown in the picture above. That is the whole story.
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by DenDev »

silver.moon wrote:Incase of JOINABLE threads inside wxThreadHelper "its released" only in wxThreadHelper destructor. So as long as the wxThreadHelper object is alive calling Wait should be always valid.
No, the thread is only valid after a successful call to CreateThread(). wxThreadHelper is used to manage the lifecycle of the thread you create with CreateThread(), you should only Wait() when you need to halt program execution until the thread has finished. It is hard to figure out what is wrong with your code since you do not provide it but the error is either a result of calling Wait() from within the thread's context or a result of calling Wait() for at thread that cannot be waited for (not started, not created etc.).
I have a bad habbit of not testing the code I post :D
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Re: Should you call Wait on a wxThreadHelper joinable thread that has stopped

Post by silver.moon »

@DenDev

Yes, you are right.
I was actually calling Wait, after already calling Delete, which was causing the error dialog.
Post Reply