C++ C++;11螺纹不工作
我的程序如下所示C++ C++;11螺纹不工作,c++,multithreading,gcc,c++11,C++,Multithreading,Gcc,C++11,我的程序如下所示 #include <iostream> #include <thread> #include <exception> void hello() { std::cout << "Hello world!!!!" << std::endl; } int main() { std::cout << "In Main\n"; std::thread t(hello); t.joi
#include <iostream>
#include <thread>
#include <exception>
void hello()
{
std::cout << "Hello world!!!!" << std::endl;
}
int main()
{
std::cout << "In Main\n";
std::thread t(hello);
t.join();
return 0;
}
#包括
#包括
#包括
void hello()
{
std::cout使用g++
编译代码时,可能需要链接pthread库,请使用-pthread
选项
下面是我从stackoverflow中找到的答案:
当我在GCC中使用C++11线程时,我使用:
g++ -std=c++0x -pthread -g main.cpp
这对我很有用。每个人都已经回答说需要将-pthread参数传递给编译器。几乎可以肯定,它在4.8中不会改变,但根据异常情况,至少会有一条很好的消息说明问题所在。您可以尝试
g++ --std=c++11 -lpthread main.cpp
它很可能不是您看到的错误消息的根本原因,但是std::cout(和其他)不应该在没有访问权限的情况下并发使用serialization@bobah-这里没有并发使用。main()
中的插入在创建线程之前完成。@PeteBecker-这正是我说“很可能不会”的原因但是为什么我们必须包含-pthread,因为它在std库中是可用的???GCC还没有自动包含pthread库。他们说它将在以后的版本中修复。你有没有链接到他们提到的地方。想读那篇文章吗?我记不起我在哪里读到的。所以,我想我没有通过所需的引用测试。请详细说明。区别是什么?为什么它提供了解决方案?
g++ -std=c++0x -pthread -g main.cpp
g++ --std=c++11 -lpthread main.cpp