I suggest not using setjmp()/longjmp() in any context where it can be avoided. Its usage is a magnet for memory leakage, and this alone is enough condemnation. For a traditional chess search there are two good alternatives:Joost Buijs wrote:In a new version of the engine I'm working on I jump right back with a long-jump to the point where I called the search when the abort flag is set, maybe this will help a little.
1) Use a flag to indicate a search stop, and test this flag upon returning from a node to toss the results. The most recently generated PV/score is set as the ultimate search result with everything calculated after the flag is set is thrown away. This is what I used to do in my search code.
2) Re-write the recursive search as a non-recursive state machine with the formerly local variables explicitly stored on a ply-indexed stack. At each state transition, the stop/abort flag can be checked and the next state will be set to the search termination state. Again, the most recently generated PV/score is set as the ultimate search result. This is what all my recent code does.
The second approach is better, although it takes some forethought to specify the state/transition map. There could be up to two or three dozen states needed with at least a pair for each implementation of what would be a single recursive call in a non-state routine. While there is some compute time needed to run the big fat switch statement on each transition, there is a gain from not having routine call/return overhead for each node. If the compiler has a decent optimizer, then there will be a lot of savings from not having a lot of register save/restore code. Further, it's much easier to debug a state machine, particularly when so much of the search information is available from an explicit stack and is not hidden away in activation records.