Hacker News new | ask | show | jobs
by Jabbles 1454 days ago
> It has to handle multiple threads reading and replacing entries, with no locking.

Is this undefined behavior or does the memory model of C++ guarantee anything given the reads/writes are presumably whole words?

I realise that the implementation doesn't actually have to be deterministic, but does the language guarantee anything in these cases?

1 comments

This implementation isn't actually race free per-se. It can happen that reads and writes overlap resulting in bad entries, but they are generally rare, since Stockfish utilises various heuristics to ensure that different threads are searching non-overlapping parts of the search tree most of the time. Erroneous information at a single node doesn't usually have a huge effect since ultimately the best move is chosen through a vote between the different threads. So it sort of cheats by avoiding most races and collisions, and then having ways of spotting bad entries, ignoring and overwriting them.

This is all a bit hand-wavey, but there you have it. You could do this with atomics by shaving down the size of an entry to 64 bits(Stockfish uses 10 bytes), I suppose.