小编m.e*_*ahi的帖子

这是g ++和clang ++优化的错误吗?

#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)

c++ optimization g++ clang++

2
推荐指数
1
解决办法
166
查看次数

标签 统计

c++ ×1

clang++ ×1

g++ ×1

optimization ×1