Jump to content
  • Advertisement
Sign in to follow this  

Threading Issues

This topic is 2182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please I need help ? sad.png
I have a main thread to run (ProcessPlayerMoves)
void StartGame()
if (mainThread == null)
mainThread = new Thread(new ThreadStart(ProcessPlayerMoves));
mainThread.Name = "My Main Thread";
mainThread.IsBackground = true;
Then the players method is as follows:
private sMove GetMoveForPlayer(Player p)
lastMove = null;
playerThread = new Thread(new ParameterizedThreadStart(p.Move));
playerThread.Name = p.Name;
//Register a listener
p.PlayerMoved += new PlayerMovedHandler(player_PlayerMoved);
// lastMove is assigned in the player_PlayerMoved handler
while (lastMove == null)
// if we get here the player moved
//Unregister the listenter
p.PlayerMoved -= player_PlayerMoved;
// Intervel Delay for the main thread 
if (chkSimulationDelay.Checked)
catch (ThreadAbortException tae)
return p.CurrentMove;
During simulation there is no consitentcy, if i want to run say 500 games it just halts at 20 or sometimes 100 or 50. And i do not get an error, the game just stops but the program is still running. Edited by swiftcoder
Please use [code][/code] tags in the future.

Share this post

Link to post
Share on other sites

Not sure how to respond here.. It seems you are using threads, yet you don't know any of the basics of threading

Memory barriers, locking, synchronization, not using sleeping, and most important of all: not spamming threads

Threads are expensive to create, and cheap to maintain


I also don't believe any of your example code is inherently parallell as you have written it, unless of course all of these are immutable objects


Seriously though, are you sure you want to go through this nightmare?

Threading isn't something programmers CHOOSE to use, it's something you use because there's no other way.

(Or you have something that is shamefully parallell that doesn't cause side-effects, which is ultra-rare)


An example:

// use 100% cpu while waiting for reference to change, all the while all cores using this cache line constantly re-synchronize

while (lastMove == null);


And a last tip: I don't know about your language, but when a thread just stops responding it:

* could be in an infinite loop

* could have crashed

* stuck on a lock

* never 'finished' completely

* waiting for signal

Edited by Kaptein

Share this post

Link to post
Share on other sites

Definately true as you put it, I do not have a background at all when it comes to threading etc.


I do not have much choice, because of the way I have decided to design my 

application (card game). Nevertheless I found a solution the my problem and

all seems to work fine.


After this project I will look extensively at how to use threading and more

specifically Parallel Programming incase for future needs.


Thanks for your input.

Share this post

Link to post
Share on other sites

You're really making things much harder for yourself trying to use threads for these games.  I guarantee you the games you are making have no NEED to use threads.


I'm a professional programmer, and I've worked with threads, and I understand how they should be used, and all the tools to use to avoid the inherent pitfalls threads come with:  Yet, for all the games I've made, I've never once used threads in them.


I've made simple board games, multiplayer poker games, multiplayer Real Time Strategy games, action games, and multi player action games, and have yet needed to use threads.  The one time I did use threads was for a multiplayer engine I wrote to handle the asynchronous incoming data from all the open sockets.  


I would HIGHLY recommend you take threads out of your design, and use the typical game loops:

while(IsRunning) {
  // update anything related to the map I'm on
 // Handle the player's Input and movements
  // handle all the enemies
  for (uint32_t i = 0; i < Enemies.size(); ++i) {
  // Now Draw Everything
  // ...


Something like that.

Share this post

Link to post
Share on other sites

BeerNutts I can see you are a pro, but when i said I had no choice meaning considering alot of things I just got some samples codes of similar 

implementation and tried to understand the appoach and it did work well for me. 


I dont mind sending you my source code and see what i am trying to do or what i have done. 


It is actually a Card game with an AI for a final project.

Edited by stoneyrich

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!