Category : compiler-optimization

Consider the following code: #include <algorithm> #include <numeric> int main() { int* v = new int[1000]; std::fill(v,v+1000,0); std::iota(v,v+1000,0); int res = v[999]; delete[] v; return res; } I would expect the compiler to see that the std::fill is not needed because the array is immediately overwritten by std::iota. However, with either Clang or GCC (with ..

Read more

#include <iostream> #include <thread> #include <mutex> int main() { std::atomic<bool> ready = false; std::thread threadB = std::thread([&]() { while (!ready) {} printf("Hello from Bn"); }); std::this_thread::sleep_for(std::chrono::seconds(1)); printf("Hello from An"); ready = true; threadB.join(); printf("Hello again from An"); } This is example from the CppCon talk https://www.youtube.com/watch?v=F6Ipn7gCOsY&ab_channel=CppCon (min 17) The objective is to first print Hello ..

Read more