Original Post
I''m writing my program (chess program) with two main threads. An input thread and a search thread. I plan on having several interfaces to my program (console, GUI, Internet Chess Server, etc.) so I chose to implement my "commands" according to the "commands" that are used to communicate on Internet Chess Servers so my program can play against other programs online. The protocol calls for various commands to be handled immediately in some cases, and if my program is halfway into a search that isn''t going to be done for another minute, it can''t very well handle the commands right away.
Anyway, I basically only need a single global flag to tell my search thread to wrap up what it''s doing and bail out. For example, if the user sends a "new game" command and the program is in the middle of a search, the search is no longer needed and can simply stop. Or if the user entered the "move now!" command, the search thread needs to report back with the best move it found so far and make that move, and stop searching.
So I really only need one little flag to communicate between the threads, and I''m attempting to make my program as portable as I can (it''s not so easy with multithreading) so using WIN32 specific solutions (Interlocked* functions) is out. I''m using pthreads in my attempt to remain as portable as possible.
My other idea was to just use a single volatile int to communicate between the two threads. Will this solution work or not? I suppose that if in my search I checked the flag several times a second, it wouldn''t matter if my search thread missed it the first time by due to some unforseen complication with the other thread changing the variable (I have no idea if this would ever happen). I could just catch the flag in another 100 ms or whatever.
If that solution will not work, do I have any other solution besides using a more expensive synchronization method such as mutexes, etc.?
Any help is appreciated.
Russell