C++ Threads, std::system_error - operation not permitted? [duplicate]

送分小仙女□ 提交于 2019-11-26 08:15:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/17274032/c-threads-stdsystem-error-operation-not-permitted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!