c++ - 链接时间优化与多线程支持冲突

标签 c++ multithreading c++11 g++ lto

当我读到有关 g++-4.9 中改进的链接时间优化支持时,我想试一试。可悲的是,我在运行时遇到异常,特别是 std::system_errore.what() == Enable multithreading to use std::thread: Operation not permitted.

现在我通常知道如何修复该错误:将 -pthread 添加到我的编译器调用中,但事实上,我已经有了这个参数!

我的示例代码是:

#include <thread>

int main()
{
   std::thread t([](){}); // do nothing in a thread!
   t.join();              // wait for nothing to be done
}

编译为(X 为 7、8 或 9)

g++-4.X -std=c++11 -pthread test.cpp -o thread_test_fine

按预期完美运行,没有运行时错误。

但是,

g++-4.X -std=c++11 -pthread -flto test.cpp -o thread_test_runtime_error

system_error 异常而失败。

问题:

此行为是有意为之(修复方法是什么?)还是错误?

(在这个问题出现之前:我的编译器都是用--enable-threads=posix构建的)

最佳答案

尝试添加这个参数:

-Wl,--no-as-needed

如果有帮助,那就是 gcc 错误:https://stackoverflow.com/a/19463892/280758

关于c++ - 链接时间优化与多线程支持冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264364/

相关文章:

c++ - 错误代码 C2676 二进制 '==' : 'std::pair<std::string,int>' does not define this operator or a conversion to a type acceptable to the predefined operato

c++ - 线程调用的函数是否对对象删除安全?

c++ - 返回成员变量时如何利用 move 语义?

c++ - 如果元素存在于对 vector 中,则在类中查找

c++ std::unique_ptr 不会在 map 中编译

c++ - ns2的c++代码中的节点在哪里处理数据包?

c++ - 如何通过结构的成员推导模板

c++ - 工具链还是 IDE? (调试问题)

c++ - 如何在 C++11 的函数内初始化线程安全的静态常量 vector ?

c# - 在压缩一个巨大的文件夹时,如何为其他应用程序提供一些磁盘时间? C#