Sign in to follow this  
cloud007

MFC Multithreading

Recommended Posts

I recently converted a Win32 application using several threads at once into MFC with CWinThread classes. Problem is, how do I get the threads to stop in a timely matter? Every time I try to close the program when several threads are running, it ends up crashing with "assertion failed" and "user breakpoint". Those errors result from threads not stopping when they should. I tried using bool variables to control execution, but that hasn't helped much. Is there a better way?

Share this post


Link to post
Share on other sites
It sounds like you could benefit from some synchronisation. Use classes like CEvent, CMultiLock or CSemaphore to synchronise your main thread and secondary threads.

When the main thread closes, ie. in ExitInstance or OnDestroy for your main window - raise an event which tells the threads to stop. Then when the thread finishes it can raise another event which the main thread will wait on to close down the application.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_multithreading_with_c.2b2b_.and_mfc.asp

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