Sign in to follow this  
stoneyrich

Threading Issues

Recommended Posts

stoneyrich    143
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;
mainThread.Start();
}
else
{
}
}
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;
playerThread.Start(game.GameBench);
 
//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)
Thread.Sleep(simulationInterval);
 
try
{
playerThread.Join(); 
}
catch (ThreadAbortException tae)
{
Console.WriteLine(tae.ToString());
}
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
Kaptein    2224

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
stoneyrich    143

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
BeerNutts    4400

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
  Map.Update();
 
 // Handle the player's Input and movements
 Player.Update();
 
  // handle all the enemies
  for (uint32_t i = 0; i < Enemies.size(); ++i) {
    Enemies[i].Update();
  }
 
  // Now Draw Everything
  // ...
}

 

Something like that.

Share this post


Link to post
Share on other sites
stoneyrich    143

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

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