I’m trying to thread a Chess AI. I have previously done this for Checkers, and the AI implementation is virtually identical. However, in the case of Chess only one of the threads ever returns, when it does all other threads are ended suddenly without finishing.
for(int i = 0; i < allMoves.Count; i++)
{
int threadNumber = i;
Thread thread = new Thread(() =>
{
Debug.Log("Starting thread " + threadNumber); // <-- called 19 times, once for each thread
values.Add(threadNumber, CalculateMoveValue(allMoves[threadNumber], maxDepth, chessController.board, threadNumber));
Debug.Log("Ending thread " + threadNumber); // <-- called only once, at which point all other threads are dead
});
thread.Start();
threads.Add(thread);
}
It runs fine when I call Thread.join (making the main thread wait) before spawning another thread or running the code on the main thread. It also works if I make CalculateMoveValue just return 0 as soon as it starts.
CalculateMoveValue simulates all possible moves up until a certain level (maxDepth) and returns the value of the worst possible outcome. The entire simulation is a bunch of static methods with all state passed in.
It’s worth noting that any errors thrown by threads won’t show up in the console unless you catch them and then log them. Could be something as simple as an error terminating the threads.
It could also be the other threads are running infinitely.
Stick in try/catch statements and see what you get back, like BoredMormon pointed out.
Also try stepping through the code if you can.
Otherwise… without seeing the code, there’s nothing we could say on the matter. You’re basically just saying “I have multiple threads, and they’re not completing.” OK…
Thanks for the tip on try/catch statements, that’s what I needed to know. Not going to dump a thousand lines of code on here and ask you guys to debug it.