Threading Issues

Started by
4 comments, last by BeerNutts 10 years, 8 months ago
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.
Advertisement

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

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.

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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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.

Go ahead and post a link to your code here, I'll take a look, but others would be able to help too.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement