Archived

This topic is now archived and is closed to further replies.

SpaceRogue

DInput multithreading issue - Help needed

Recommended Posts

I'm trying to add simple multithreading to my game to remove the need for the windows message pump in my game's main loop. I've followed the instructions in the article: http://www.gamedev.net/reference/articles/article1249.asp My code is much like the code in the article, and everything works fine until I try to initialize direct input (in my game loop thread, not my initial thread containing the message pump...Is this a problem?). My direct input code is taken from the 'action basic' sample and works absolutely fine (when not using multiple threads). When I try to use multiple threads, my game locks up about 75% of the time. (Isn't this an indicator of a possible race condition?) The thing is, the only variable used by BOTH threads is my gamestate enumerated type to check if the game is ending and I have used critical sections in all of the places that is used. Anyway, I'm sure DInput causes the problem...commenting out the init code for DInput makes the game run flawlessly everytime, yet the problem remains if I initialize DInput and comment out the actual Get() function that updates my input each frame. So, it is definitely DInput's initialization. The biggest obstacle to figuring this out has been the fact that EVERYTIME I run the game in the debugger, it works! Argh! Any helpful hints on how I could figure out what the problem is? I'll post some code if anyone thinks it'll help. Edit: I noticed that in task manager I have 3 threads when the game is working (initial, game loop, and dinput creates its own apparently). When the game 'hangs' task manager reports only two...I presume dinput is failing to create its own thread. Additionally, if I put a simple Sleep(1000) statement right above the line that creates my dinput device, the game ALWAYS works. It seems any kind of 'delay' during startup causes dinput to hang during initialization. I noticed running the .exe from a network share causes the problem to be more pronounced than running it from a local drive, which is consistent with that thinking. For now, the Sleep hack has everything working, but I'd like to know what is really causing this...any ideas on how to find out? [edited by - SpaceRogue on March 17, 2003 6:01:18 AM]

Share this post


Link to post
Share on other sites
Ok, more testing shows that when the game ''hangs'', the game will eventually become responsive after ~30 seconds and continue to work normally. It seems very odd the DInput would take this much time to initialize.


Share this post


Link to post
Share on other sites
Without seeing any code, it''s a little difficult to pin point the exact problem. Why exactly are you doing all of your initialization in separate threads? Why not perform all of your DX (graphics and input) initialization in the main thread, and then create your input thread?

Share this post


Link to post
Share on other sites
I''m heading to work right now...but I''ll post the code a little later.

Essentially my initial thread does the following: create a window, launch the ''game'' thread containing the game''s main loop and then sit back and process windows messages.

My game loop thread initializes d3d, dinput, dmusic, etc. and enters the loop.

I have only two threads...but I noticed DInput automatically creates a thread of its own...perhaps because I''m using action mapping? I could move Dinput''s initialization to the initial thread, but it would be a big pain in the butt and I fail to see why it should be necessary.

Hopefully I''ll have some code up in a couple hours...

Share this post


Link to post
Share on other sites
I couldn''t find a way to easily reduce my code to a snipet that would still exhibit the problem, so I gave up and moved the DX initialization code to the initial thread. Everything works fine now.

Becuase it eventually worked (albeit with a significant delay) I am pretty confident it wasn''t my code, but was something about DInput itself.

Share this post


Link to post
Share on other sites