The program below uses thread-safe static initialization to implement a singleton.
When analyzing this with Helgrind, a data race is reported between the initialization of "val" in the constructor (executed by thread 1) and the increment operation (executed by thread 2). This is expected, as Helgrind does not "understand" the synchronization generated by the compiler.
I'm looking for a way to get rid of the false positives in such cases, and experimented with annotations. I found that placing
inside the getInstance() method (either in this order or reversed) makes the Helgrind warnings disappear.
Is this a valid approach? Are there possible side-effects? Could this "break" the analysis and lead to other data races going undetected?