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

C++MultithreadingC++11Std System-Error

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreelanceConsultantView Question on Stackoverflow
Solution 1 - C++billzView Answer on Stackoverflow