#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 I compiled the above code with following commands, everything was good:
$ g++ test.cpp -lpthread -std=c++11
$ clang++ test.cpp -lpthread -std=c++11
But when I tried to use optimization flags, my program didn't finish. I tested all 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 the versions of my g++ and clangs were:
$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A 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