c++ - Is it a bug for g++ and clang++ optimization? -
#include <unistd.h> #include <pthread.h> #include <stdio.h> bool m_ok = false; void* run(void*) { usleep(1000000); m_ok = true; printf ("good bye!\n"); return nullptr; } int main() { pthread_t my_thread; pthread_create(&my_thread, nullptr, &run, nullptr); while (!m_ok) continue; printf("yes!!!\n"); return 0; }
when compiled above code following commands, good:
$ g++ test.cpp -lpthread -std=c++11 $ clang++ test.cpp -lpthread -std=c++11
but when tried use optimization flags, program didn't finish. tested of following commands:
$ g++ test.cpp -lpthread -std=c++11 -o1 $ clang++ test.cpp -lpthread -std=c++11 -o1 $ g++ test.cpp -lpthread -std=c++11 -o2 $ clang++ test.cpp -lpthread -std=c++11 -o2
also versions of g++ , clangs were:
$ g++ --version g++ (ubuntu/linaro 4.8.1-10ubuntu9) 4.8.1 copyright (c) 2013 free software foundation, inc. free software; see source copying conditions. there no warranty; not merchantability or fitness particular purpose. $ clang++ --version debian clang version 3.2-7ubuntu1 (tags/release_32/final) (based on llvm 3.2) target: x86_64-pc-linux-gnu thread model: posix
no. c , c++ memory models not permit cross-thread interactions non-atomic variables, or when not protected mutex/lock. data race , in violation of abstract machine, optimizer within rights launch nethack.
the optimizer written assume, explicitly permitted standard, external sources (including other threads) cannot magically mutate program's state without explicit control user.
Comments
Post a Comment