OK, I'm new to AI, so please be gentle. I am writing a chess program (not exactly chess, but perfect info nonetheless.) I am trying to get multithreading working, so my whole app doesn't go catatonic while it's thinking.
I am programming with MFC (needed to support other features in the whole app) with Document/View architecture, using VC++6, running on an XP Home machine.
I have class CCujo, which takes the current board state, and can figure everything else from there. It also has two member variables, char's which hold the numbers of the board spaces affected by the move (one is the location of the piece that needs to move, the other where it's headed.) After the minimax/alpha-beta search, these two contain the info for least worst move found.
I am running into the problem of finding out when the thread has terminated, then extracting the move info from it.
The last idea I gave up on was using a struct that includes three things: the "From" and "To" spaces and a bool that indicates the thread hasn't yet termed. I initialize all the members of that struct in my calling class, then pass a pointer to that struct as a member variable of CCujo (the "brain"). When CCujo exits the move-search function at the top level, it sets the bool bit to TRUE, to indicate that the function is finished, and so the thread is done. The calling class checks every half-second or so to see if this bool is set to TRUE. (The thinking here is that anything done to a variable's pointer by a function is done to the variable. This is probably where I'm going wrong.)
The problem I am running into with this approach (which, quite frankly, is the best I've come up with) is if the search function is not finished by the first time the calling class checks that bool, the thread will eventually term, but that bool will never be set to TRUE, and the calling class just continuously checks, w/o ever getting the info it's going for, nor the command to stop looking.
But wait, it gets weirder...
When I do not execute the search as part of a new (worker) thread, it gives me moves that make sense. As a separate thread, however, through the use of TRACE macro, I am able to see the moves it's coming up with, and it's doing things it's not supposed to do. (Leaving a very important piece threatened and undefended, for example.) Perhaps the search gets cutoff early, or by accessing the struct with the calling class, I somehow block any further access by CCujo to that struct? (Just ideas, I'm sure they're wrong, built on complete conjecture as they are.)
I'm sure there is something fundamental about multithreading, passing pointers to structs into classes used in threads, etc. that I'm not understanding. Could someone take just a couple of seconds and tell me what I'm missing, or if it's a little more complicated than that, at least nudge me in the right direction?
Even a nicely-worded "blow it out an orifice" would do. Just an answer.
(I am only too happy to post code. Just ask, and I'll figure out how to do it.)