Open Quuxplusone opened 3 years ago
The warning message is actually not wrong: the code contains a data race, the compiler is allowed to replace the read operation of keep_running
with a constant and remove the read of keep_running_thread
.
Oh, interesting! I'm happy to have this bug closed then. If you're able to point me to the documentation or portion of the standard that explains this, that would be very helpful for me, just for my own learning experience. Thanks!
(In reply to Sam Kearney from comment #2)
> Oh, interesting! I'm happy to have this bug closed then.
Leave it open. The warning message could be improved.
(In reply to Sam Kearney from comment #2)
> If you're able to
> point me to the documentation or portion of the standard that explains this,
> that would be very helpful for me, just for my own learning experience.
[intro.races] p21 (https://eel.is/c++draft/intro.races#21)
To reproduce: Install LLVM 14.0, add this minimal example as main.cpp:
Run clang-tidy --checks=clang-analyzer-* main.cpp. It reports:
main.cpp:22:3: warning: Value stored to 'keep_running_thread' is never read [clang-analyzer-deadcode.DeadStores] keep_running_thread = false; ^
~main.cpp:22:3: note: Value stored to 'keep_running_thread' is never read keep_running_thread = false; ^~I would expect no warning here since a reference to keep_running_thread is passed to the thread.
Note: You can work around this by using a capturing lambda that captures keep_running_thread by reference; but I would expect this to work using the method shown as well.