Volatile keyword in DCL

Sonar Rule RSPEC-3077 (SonarSource Code Analyzers Rules Explorer) is also reported when someone uses Double Checked Locking.

RSPEC-2168 (SonarSource Code Analyzers Rules Explorer) even mentions using volatile as a compliant solution to ensure Double Checked Locking is Thread Safe.

Isn’t RSPEC-3077 a violation of RSPEC-2168?

Double checked Locking with volatile keyword prevents against both instruction reordering and memory visibility to different threads.
It is also mentioned as a correct solution for multi threading on JDK 5+ : The "Double-Checked Locking is Broken" Declaration

So, RSPEC-3077 should not be reported when it is used in the context of Double checked locking.

Hello @shubhamgarg1

Thanks for reporting the issue. It indeed looks like RSPEC-3077 should not report issues on volatile variables used in double-checked locking.

A ticket has been created to handle the issue.

Cheers,

Dorian