C ++线程,std :: system_error - 不允许操作? [英] C++ Threads, std::system_error - operation not permitted?
问题描述
所以我写了一个程序来测试线程在64位kubuntu linux,版本13.04。实际上,我从编写测试程序的人那里抢了代码。
#include< cstdlib>
#include< iostream>
#include< thread>
void task1(const std :: string msg)
{
std :: cout< task1说:< msg< std :: endl;
}
int main(int argc,char ** argv)
{
std :: thread t1(task1,Hello);
t1.join();
return EXIT_SUCCESS;
}
我编译时使用:
g ++ -pthread -std = c + + 11 -c main.cpp
g ++ main.o -o main.out
然后运行:
./ main。 out
另外,当我'ls -l'时,main.out显示为绿色文本像所有可执行文件,但在其名称的末尾还有一个星号。为什么是这样?
回到问题手头:当我运行main.out时,出现一个错误,说:
在抛出'std :: system_error'的实例后调用终止
what():不允许操作
中止(内核转储)
有人如何解决这个问题?
您没有正确链接pthread,请尝试以下命令(注意:order matters)
g ++ main.cpp -o main.out -pthread -std = c ++ 11
p>
使用两个命令执行它
g ++ -c main.cpp -pthread -std = c ++ 11 //生成目标对象文件
g ++ main.o -o main.out -pthread -std = c ++ 11 //链接到目标二进制
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.
#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?
Back to the problem in hand: When I ran main.out, an error appeared, which said:
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?
You are not linking pthread properly, try below command(note: order matters)
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
这篇关于C ++线程,std :: system_error - 不允许操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!