这是g++和clang++优化的bug吗?
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;
}
当我用以下命令编译上面的代码时,一切正常:
$ g++ test.cpp -lpthread -std=c++11
$ clang++ test.cpp -lpthread -std=c++11
但是当我尝试使用优化标志时,我的程序没有完成。我测试了以下所有命令:
$ 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
我的g++和clangs的版本是:
$ 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
No。C和c++内存模型不允许非原子变量的跨线程交互,或者在没有互斥锁保护的情况下。这是一场数据竞赛,违反了抽象机器,所以优化器完全有权利启动《Nethack》。
优化器的编写假定,正如标准明确允许的那样,外部源(包括其他线程)不能在没有用户显式控制的情况下神奇地改变程序的状态。
相关文章:
- 空基优化子对象的地址
- 关闭||运算符优化
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- Clang bug?使用指针作为模板参数
- 返回值优化:显式移动还是隐式
- 人脸跟踪arduino代码的优化
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 纯函数,为什么没有优化
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 如何以优化的方式同时迭代两个间距不相等的数组
- 小字符串优化(调试与发布模式)
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 浮点定向舍入和优化
- Visual Studio 调试优化如何工作?
- 为什么开关的优化方式与 c/c++ 中的链接不同?
- 线性优化目标函数中的绝对值
- 签名:GCC 中的 NESS 优化"bug"
- 这是g++和clang++优化的bug吗?
- g++优化小写到大写- bug
- 优化编译器消除bug