Multithreaded problem
Hi
I currently have my main game loop running in its own thread and the message pump running in the main loop, the only problem is, when I exit the game, my thread is still running and I get all sorts of access violations on objects that I release during my exit routine. What is the proper way to end a thread? I have tried just closing the handle, and even settin boolean values in the game loop function, but because it executes in parallel, the values don''t get updated correctly. Any ideas?
Kevin =)
-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
IIRC, ExitThread or TerminateThread
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
Exit Thread can only be called from inside a the Thread(or from what I can tell). What I found on the MSDN is that I can post a message to the USERDATA segment of the Window''s LONG Value and test for that in the Thread Function, then I can use WaitForSingleObject to wait for the thread to finish. Once I have gotten past that, program cleans up nicely.
Kevin =)
-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com
Kevin =)
-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
grass:
When you create a thread, you''re allowed to pass an argument to it. This argument is usually a pointer to a structure containing everything the thread needs to know to run. In your case, you should include a pointer to a flag "pEndThread". It needs to be a pointer so the winproc thread and the game thread can see the same memory location. The flag pointed-to by this pointer is cleared when the thread starts. Your game thread should check this flag intermittently. When your winproc gets the request to exit, it should set this flag and then WaitForSingleObject on the thread handle. That should clear it all up.
Generally, TerminateThread doesn''t work very well because it doesn''t give your worker thread a chance to clean up after itself. Its best to let the thread end itself.
When you create a thread, you''re allowed to pass an argument to it. This argument is usually a pointer to a structure containing everything the thread needs to know to run. In your case, you should include a pointer to a flag "pEndThread". It needs to be a pointer so the winproc thread and the game thread can see the same memory location. The flag pointed-to by this pointer is cleared when the thread starts. Your game thread should check this flag intermittently. When your winproc gets the request to exit, it should set this flag and then WaitForSingleObject on the thread handle. That should clear it all up.
Generally, TerminateThread doesn''t work very well because it doesn''t give your worker thread a chance to clean up after itself. Its best to let the thread end itself.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement