【问题标题】:instant segmentation fault with debian and std::thread c++debian 和 std::thread c++ 的即时分段错误
【发布时间】:2012-08-21 14:49:29
【问题描述】:

遇到了这个问题 - 在标题中..

我有这个代码:

#include <thread>
#include <iostream>

void my_thread_func()
{
    std::cout<<"hello"<<std::endl;
}

int main()
{
    std::thread t(my_thread_func);
    t.join();
}

取自网络。编译器选项 -pthread -std=gnu++0x (也试过-std=c++0x)我有段错误。一切都在 vmBox 上的 Debian 上。我之前已经启动了其他代码,并且它们有效。突然,我在所有工作应用程序中使用 std::thread 的线程出现了段错误。

编辑:这是来自 gdb:

(gdb) where
#0  0x00000000 in ?? ()
#1  0x08048dc9 in thread<void (*)()> (this=0xbffff3fc, 
    __f=0x8048b9f <my_thread_func()>) at /usr/include/c++/4.4/thread:129
#2  0x08048bea in main () at ../test.cpp:18

(当我使用 std::thread t(&ClassName::my_thread_func,ptr) 启动更高级的应用程序时,错误相同,但另一行 [thread:133])

我在网上搜索,但没有找到合适的。

【问题讨论】:

  • 与 boost::thread 一起工作?什么版本的gcc?还有看看/usr/include/c++/4.4/thread:129怎么样?
  • 我在 Ubuntu 11.04 上尝试过,对我来说效果很好。没有段错误。
  • g++ (Debian 4.4.5-8) 4.4.5 ,没有检查 boost
  • 您是否收到“Pure virtual function called”消息?
  • @Wiciu 实际版本是 4.7。关于你的版本检查gcc.gnu.org/gcc-4.4/cxx0x_status.html

标签: c++ multithreading debian stdthread


【解决方案1】:

g++ -std=c++0x -lpthread 编译。注意l 之前的pthread

【讨论】:

    【解决方案2】:

    Gcc 4.4 对 C++11 特性的支持极其有限,尤其是在线程领域。由于库尚未准备好,我在 gcc 4.4 和 4.5 中看到了很多与线程相关的崩溃。

    我很确定这是一个编译器“错误”*,建议您至少升级到 gcc 4.7。

    *“bug”,因为 gcc 4.4 从未声称完全支持std::thread,因此你不能指望它能够正常工作。

    编辑:我刚想到的一件事是,有时静态链接到 glibc 和/或 libpthread 在旧版本中不起作用。

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多