Are “data races” and “race condition” actually the same thing in context of concurrent programming

No, they are not the same thing. They are not a subset of one another. They are also neither the necessary, nor the sufficient condition for one another. The definition of a data race is pretty clear, and therefore, its discovery can be automated. A data race occurs when 2 instructions from different threads access … Read more

Multithreading program stuck in optimized mode but runs normally in -O0

Two threads, accessing a non-atomic, non-guarded variable are U.B. This concerns finished. You could make finished of type std::atomic<bool> to fix this. My fix: #include <iostream> #include <future> #include <atomic> static std::atomic<bool> finished = false; int func() { size_t i = 0; while (!finished) ++i; return i; } int main() { auto result=std::async(std::launch::async, func); std::this_thread::sleep_for(std::chrono::seconds(1)); … Read more