Deleting the thread after completing the cycle [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.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2517
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Deleting the thread after completing the cycle [SOLVED]

Postby ONEEYEMAN » Sat Jun 23, 2018 6:04 am

Hi, ALL,
I'm following an example from the documentation for the detached thread.
Thread works fine, however problem arises when the application ends.

Here is the pseudo-code:

Code: Select all

wxThread::ExitCode Foo::Entry()
{
    std::vector<std::wstring> errorMsg;
    while( !TestDestroy() )
    {
// function call
    }
    return (wxThread::ExitCode) 0;
}


I did implement the thread deletion logic inside OnClose() as in the documentation. What I'd like to avoid is calling wxThread::Delete() in the middle of the "function_call". Something like this:

Code: Select all

wxThread::ExitCode Foo::Entry()
{
    std::vector<std::wstring> errorMsg;
    while( !TestDestroy() )
    {
        m_safeDelete = false;
// function call
        m_safeDelete = true;
        Sleep( 60 );
    }
    return (wxThread::ExitCode) 0;
}


I can make a member variable inside the thread class and check if its safe to delete the thread object and check if this flag is set as in the code above. But maybe such mechanism is already exists?
Last edited by ONEEYEMAN on Tue Jun 26, 2018 2:26 pm, edited 1 time in total.

User avatar
doublemax
Moderator
Moderator
Posts: 12423
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deleting the thread after completing the cycle

Postby doublemax » Sat Jun 23, 2018 7:45 am

I can make a member variable inside the thread class and check if its safe to delete the thread object and check if this flag is set as in the code above.
I don't see what difference that would make. If you call wxThread::Delete(), the thread doesn't get killed immediately. It only sets an internal flag which is checked with TestDestroy(). A detached thread will destroy (as in delete) itself when it terminates. (After you return from Entry()).
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2517
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Deleting the thread after completing the cycle

Postby ONEEYEMAN » Sat Jun 23, 2018 3:54 pm

doublemax,
Maybe in this case I'm better off with the joinable thread? At least I will be in control when it will be deleted.

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 12423
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deleting the thread after completing the cycle

Postby doublemax » Sat Jun 23, 2018 4:31 pm

Maybe in this case I'm better off with the joinable thread?
If the thread uses resources that could be deleted too early, that's an option.
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2517
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Deleting the thread after completing the cycle

Postby ONEEYEMAN » Sat Jun 23, 2018 5:38 pm

doublemax,
No, it does not.
But with current code I have a not released pointer, because the call to Delete was executed in the middle of the some function call.
That's why I'd like to have a flag indicating that the function finished and its ok to delete the thread with Delete() call.

Now this function is a member of another class which is a member of my thread class. So its like this:

Code: Select all

class Foo : public wxThread
{
private:
    MyClass m_bar;
}

wxThread::ExitCode Foo::Entry()
{
    std::vector<std::wstring> errorMsg;
    while( !TestDestroy() )
    {
        m_bar->Bar( errorMsg );
    }
    return (wxThread::ExitCode) 0;
}


And so if the call to Delete() comes during the Bar() execution I will have a problem.

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 12423
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deleting the thread after completing the cycle

Postby doublemax » Sat Jun 23, 2018 7:16 pm

And so if the call to Delete() comes during the Bar() execution I will have a problem.
Sorry, i still don't see why that's a problem. The Delete() call does not immediately kill the thread.
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2517
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Deleting the thread after completing the cycle

Postby ONEEYEMAN » Sat Jun 23, 2018 7:22 pm

doublemax,
I don't know. I guess its just a timing issue.
But I think I might have a solution. I will do a clean-up in two places: when I don't need the resource and in the thread class destructor, making the resource a class member. That way everything should be good.

Thank you.


Return to “C++ Development”

Who is online

Users browsing this forum: Google [Bot] and 6 guests