c++ - std::thread 无法在 linux eclipse 上工作

标签 c++ linux eclipse multithreading c++11

我不知道为什么,但是在将 c++11 添加到 eclipse 中的项目并使用 std::array<> 等新功能之后,当我使用 std::thread 时它突然无法工作。

这是我尝试运行的示例:

#include <iostream>
#include <string>
#include <thread>

void print_message_function(const std::string& msg);

int main()
{
    std::string message1 = "Thread 1";
    std::string message2 = "Thread 2";

    std::thread thread1(print_message_function, message1);
    std::thread thread2(print_message_function, message2);

    thread1.join();
    thread2.join();
}

void print_message_function(const std::string& msg)
{
    std::cout << msg << std::endl;
}

编译时没有错误,运行时也没有错误(也没有输出..),但是当使用调试工具时,它在这一行崩溃:

std::thread thread1(print_message_function, message1);

这是崩溃时的堆栈:

Thread [1] (Suspended: Signal 'SIGSEGV' received. Description: Segmentation fault.) 
    5 _dl_fixup()  0x0000003d6920df7c   
    4 _dl_runtime_resolve()  0x0000003d69214625 
    3 std::thread::_M_start_thread()  0x0000003d762b65a7    
    2 std::thread::thread<void (&)(std::string const&), std::string&>() /usr/include/c++/4.4.4/thread:133 0x0000000000402268    
    1 main() /.../Main.cpp:12 0x0000000000401e8d    

为什么会发生这种情况?

最佳答案

您需要在编译命令中使用 -pthread 链接到 pthreads 库。

关于c++ - std::thread 无法在 linux eclipse 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20773907/

相关文章:

c++ - Windows:从语言环境字符串获取 LCID?

c++ - 我可以(应该)将我的 DBClientConnection 用作全局变量吗?

php - UTF-8贯穿始终

eclipse - Eclipse中JSP的格式化规则

python - 导入错误 : No module named . .. [请完整解决方案]

c++ - C++中整数的十进制、八进制和十六进制表示

linux - 容器以特权模式运行

基于linux内核编程更新路由表

java - Windows 上的 Hadoop 在启动数据节点时出现 Eclipse 错误

c++ - 如何使用 Doxygen 提供类的示例代码?