What thread type to use?

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
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

What thread type to use?

Post by ONEEYEMAN »

Hi,
In wx there 2 types of threads - joinable and detached.
The detached thread deletes itself, while joinable is not and you have to delete it.

Now in my program I'm trying to create a detached thread - I figured it would be easier. I also followed the sample code that is available in the wxThread "Types of wxThread" section.

But it looks like the thread takes more time to be killed. Basically what happen is:
1. Program starts.
2. User press the button and the program connects to the database.
3. After successful connection the program launches a thread, which continuously queries the database using the same connection object.
4. User presses the "X" button.
5. OnClose handler is executed where the thread should be deleted.
6 Main frame destructor is called where the program disconnects.

The problem is that with this scenario I'm getting an error that there is still an open connection. My guess is that the thread is not yet finished, but the disconnect call is already executing.

Now, I do mix up the std::mutex and wxCriticalSection as thrread start function is using the former and the disconnect call using the latter, but does it matter. They both doing the same thing - locking access to the resources.

Now what I'm thinking is to create a joinable thread, where you can Wait() for it to finish and the safely disconnect.
Or maybe there is a better choice?

Thank you.
Post Reply