Sign in to follow this  
Antrim

Multithreading Termination

Recommended Posts

Now that I isolated and solved a problem I asked about in another thread (which is titled very irrelevantly to the issue), I'm wondering if there is a more preferred method doing what I did. My engine uses the main thread to initialize all the engine components, then starts a new thread for the game state and allows the message pump to run in the main loop. The issue I was having was if someone closed the window itself using the "X" button, I would occasionally get JIT debugging errors for an invalid PID. This was a result of the main thread closing without killing the game thread first. My solution was to do the following: In my message pump: case WM_DESTROY: // kill the app TerminateThread( g_gameThread, 0 ); WaitForSingleObject( g_gameThread, INFINITE ); PostQuitMessage(0); return(0); and then in my game thread: while(alive) { // Do Game Stuff } PostMessage( hWnd, WM_CLOSE, 0 ); return 0; Now everything works fine (at least as far as I can tell). So my question is if there is a more preferred way to get the game thread to shut down than this? Are there any potential pitfalls from this method? I'm pretty sure that I'm brute forcing a close of the thread, but this shouldn't be a problem since the forceful close of the window would put me in a state where I couldn't really do any cleanup anyway right? Thanks for any comments.

Share this post


Link to post
Share on other sites
If there's no chance that the thread will be doing some useful operation that it needs to finish and/or clean up from, this method is fine. To be more polite to the thread, you can use a volatile "should terminate" flag which the thread checks when it has nothing better to do; quitting then consists of setting the flag and waiting on the thread.

WARNING: If the thread is not constantly cycling, but is rather a sleeping worker thread, this can cause a deadlock. To avoid this deadlock, use a condition variable instead of a flag, and have the thread wait on the condition variable as well as whatever else it's waiting for.

Share this post


Link to post
Share on other sites
You shouldn't call TerminateThread. Not ever.

The reason is, it might be in the middle of something which has taken a lock, which will subsequently cause the main thread (or some other thread) to deadlock.

The correct way of doing it is to set a flag and/or wake up the thread to ask it to terminate, then wait for it to do so (like Thread.join).

Mark

Share this post


Link to post
Share on other sites
Ahhhh, excellent thanks for the info. I knew something felt wrong about that method. There isn't anything (at the moment) that I should have to worry about, but better to be safe than sorry.

Just gave my engine a static bool member and set that instead of the TerminateThread.

Thanks for the advice!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this