C++ 线程,std::system_error - 不允许操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17274032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Threads, std::system_error - operation not permitted?
提问by user3728501
So I wrote a program to test threads on 64 bit kubuntu linux, version 13.04. Actually I robbed the code from someone else who was writing a test program.
所以我写了一个程序来测试 64 位 kubuntu linux 版本 13.04 上的线程。实际上,我从正在编写测试程序的其他人那里窃取了代码。
#include <cstdlib>
#include <iostream>
#include <thread>
void task1(const std::string msg)
{
std::cout << "task1 says: " << msg << std::endl;
}
int main(int argc, char **argv)
{
std::thread t1(task1, "Hello");
t1.join();
return EXIT_SUCCESS;
}
I compiled using:
我编译使用:
g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out
Then ran:
然后跑:
./main.out
As an aside, when I 'ls -l', main.out shows up in in green text like all executables, but also has an asterisk at the end of its name. Why is this?
顺便说一句,当我使用“ls -l”时,main.out 像所有可执行文件一样以绿色文本显示,但在其名称末尾还有一个星号。为什么是这样?
Back to the problem in hand: When I ran main.out, an error appeared, which said:
回到手头的问题:当我运行main.out时,出现了一个错误,它说:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Anyone any ideas on how to fix this?
有人对如何解决这个问题有任何想法吗?
回答by billz
You are not linking pthread properly, try below command(note: order matters)
您没有正确链接 pthread,请尝试以下命令(注意:顺序很重要)
g++ main.cpp -o main.out -pthread -std=c++11
OR
或者
Do it with two commands
用两个命令来做
g++ -c main.cpp -pthread -std=c++11 // generate target object file
g++ main.o -o main.out -pthread -std=c++11 // link to target binary