AngouriMath

Navigation

← Back to list of members

factorialCache

 Field

Summary

The factorials computed so far, as a snapshot nobody mutates: [i] is i!.

Remarks

This was a List`1 grown in place under a lock, read without one. That is
not a safe pattern for List`1 and the failure is silent rather than an
exception: Add reallocates the backing array, so a reader outside the lock can
see the newCount against the old array and return a value for
a different index — a wrong factorial, well-formed and enormous, from a method that
cannot fail. The last return was outside the lock as well.
An array published by a single reference write fixes it without making readers wait.
A reader takes the reference once — that read is atomic — and then indexes an array
that is never written again, so it sees either the old complete snapshot or the new
one and never a half-grown one. The shared IEnumerator`1 that used to
carry the running product went with it: one enumerator advanced from several threads
is the same defect in a second place.

























Angouri © 2019-2023 · Project's repo · Site's repo · Octicons · Transparency · 4378 pages online