FactomProject / factomd

Factom Daemon
https://www.factomprotocol.org/
Other
201 stars 92 forks source link

[Nowax] Make RateCalculator thread consistent #1097

Closed WhoSoup closed 3 years ago

WhoSoup commented 3 years ago

The Ratecalculator was technically thread safe, as in it didn't crash when accessed by multiple threads, but it wasn't "thread consistent", since it was theoretically possible to update the values while in the middle of loop, making the latter yield inconsistent results.

This change makes the rate calculator use a mutex instead of atomically swapping uints independently so that a single loop execution is always consistent.

PaulBernier commented 3 years ago

The generally accepted definition of "thread safety" is not that the code doesn't crash in concurrency context but that the correctness of the code is not affected (in particular yields predictable results). So this code was not thread-safe at all... Thanks for fixing that.