#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;
}
Run Code Online (Sandbox Code Playgroud)
当我用以下命令编译上面的代码时,一切都很好:
$ g++ test.cpp -lpthread -std=c++11
$ clang++ test.cpp -lpthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用优化标志时,我的程序没有完成.我测试了以下所有命令:
$ 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
Run Code Online (Sandbox Code Playgroud)
我的g ++和clangs的版本也是:
$ g++ --version …Run Code Online (Sandbox Code Playgroud)