c++ 11线程不工作[重复]

时间:2020-12-04 21:01:38

This question already has an answer here:

这个问题已经有了答案:

My programs looks like below

我的程序如下所示

#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;
}

When I compile it using the following command I get no errors

当我使用以下命令编译它时,我不会得到错误

g++-4.7 -std=c++11 main.cpp

But when I run it I get the following error

但是当我运行它时,我得到如下的错误

In Main
terminate called after throwing an instance of 'std::system_error'
what():  Operation not permitted
Aborted (core dumped)

Could someone help me with where I am going wrong?

有人能帮我解决我的问题吗?

5 个解决方案

#1


10  

When I use C++11 threads with GCC, i use:

当我使用GCC的c++ 11线程时,我使用:

g++ -std=c++0x -pthread -g main.cpp

That works for me.

这适合我。

#2


6  

When compiling your code with g++, use the -pthread option.

使用g++编译代码时,使用-pthread选项。

Below is the answer I find from *: In g++ is C++ 11 thread model using pthreads in the background?

下面是我从*上找到的答案:在g++中,c++是使用后台pthreads的c++ 11线程模型吗?

#3


3  

Everybody already answered that you need the -pthread argument passed to the compiler. Almost for sure it won't change in 4.8 but according to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681 the exception will at least have a nice message stating what's wrong.

每个人都已经回答过需要将-pthread参数传递给编译器。几乎可以肯定的是,它在4.8中不会改变,但是根据http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681异常至少会有一个很好的消息来说明哪里出错了。

#4


0  

You might need to link against the pthread library

您可能需要链接到pthread库

#5


0  

You can try with,

你可以试试,

g++ --std=c++11 -lpthread main.cpp

#1


10  

When I use C++11 threads with GCC, i use:

当我使用GCC的c++ 11线程时,我使用:

g++ -std=c++0x -pthread -g main.cpp

That works for me.

这适合我。

#2


6  

When compiling your code with g++, use the -pthread option.

使用g++编译代码时,使用-pthread选项。

Below is the answer I find from *: In g++ is C++ 11 thread model using pthreads in the background?

下面是我从*上找到的答案:在g++中,c++是使用后台pthreads的c++ 11线程模型吗?

#3


3  

Everybody already answered that you need the -pthread argument passed to the compiler. Almost for sure it won't change in 4.8 but according to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681 the exception will at least have a nice message stating what's wrong.

每个人都已经回答过需要将-pthread参数传递给编译器。几乎可以肯定的是,它在4.8中不会改变,但是根据http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681异常至少会有一个很好的消息来说明哪里出错了。

#4


0  

You might need to link against the pthread library

您可能需要链接到pthread库

#5


0  

You can try with,

你可以试试,

g++ --std=c++11 -lpthread main.cpp