0

I have a C++11 program that gives me this error:

terminate called after throwing an instance of 'std::system_error'
what():  Operation not permitted

Code:

const int popSize=100;
void initializePop(mt3dSet mt3dPop[], int index1, int index2, std::string ssmName, std::string s1, std::string s2, std::string s3, std::string s4, std::string mt3dnam, std::string obf1, int iNSP, int iNRM, int iNCM, int iNLY, int iOPT, int iNPS, int iNWL, int iNRO, int ssmPosition, int obsPosition ){
  if((index1 >= index2)||index1<0||index2>popSize){
    std::cout<<"\nInitializing population...\nIndex not valid..\nQuitting...\n";
    exit(1);
  }
  for(int i=index1; i<index2; i++){
    mt3dPop[i].setSSM(ssmName, iNSP, iNRM, iNCM, iNLY);
    mt3dPop[i].setNam(toString(s1,s3,i));
    mt3dPop[i].setObsName(toString(s1,s4,i));
    mt3dPop[i].setSsmName(toString(s1,s2,i));
    mt3dPop[i].getSSM().generateFl(toString(s1,s2,i),iOPT,iNPS);
    mt3dPop[i].generateNam(mt3dnam, ssmPosition, obsPosition);
    mt3dPop[i].setFitness(obf1, iNWL, iNRO);
  }}

void runPackage(ifstream& inFile){
//all variables/function parameters for function call are read from inFile
unsigned int numThreads = std::thread::hardware_concurrency();// =4 in my computer
std::vector<std::thread> vt(numThreads-1);//three threads
for(int j=0; j<numThreads-1; j++){
    vt[j]= std::thread(initializePop,mt3dPop,j*popSize/numThreads, (j+1)*popSize/numThreads, ssmName, s1,s2, s3, s4, mt3dnam,obf1,iNSP, iNRM, iNCM, iNLY, iOPT, iNPS, iNWL, iNRO, ssmPosition, obsPosition );   //0-24 in thread 1, 25-49 in thread 2, 50-74 in thread 3            
}
//remaining 75 to 99 in main thread
initializePop(mt3dPop,(numThreads-1)*popSize/numThreads, popSize, ssmName, s1,s2, s3, s4, mt3dnam,obf1,iNSP, iNRM, iNCM, iNLY, iOPT, iNPS, iNWL, iNRO, ssmPosition, obsPosition);        

for(int j=0; j<numThreads-1; j++){
    vt[j].join();
}}

What does the error mean and how do I fix it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
George
  • 91
  • 1
  • 3
  • 8

2 Answers2

2

You need to link correctly, and compile with -std=c++11 - see this example.

I'm guessing you had the same problem as me! (I compiled with -pthread and -std=c++11 rather than linking with those two. (But you will need to compile with std=c++11 as well as linking with it.))

Probably you want to do something like this:

g++ -c <input_files> -std=c++11

then

g++ -o a.out <input_files> -std=c++11 -pthread

... at least I think that's right. (Someone to confirm?)

Community
  • 1
  • 1
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • I was doing it in NetBeans IDE. Another similar code worked fine. I am using C++11 (selected that option in NetBeans) – George Sep 01 '13 at 17:45
  • @George Did it link correctly this time? I assume you can set a linker flag in netbeans? – FreelanceConsultant Sep 01 '13 at 17:48
  • I did not have to use the -pthread flag explicitly in another similar program. But tried that also now. Same result. – George Sep 01 '13 at 17:58
  • Now it is working...but I didn't do anything. Just recompiled and it is running..But I don't understand why it didn't work earlier. It is bothering me – George Sep 01 '13 at 18:42
0

How to reproduce these errors:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
void task1(std::string msg){
  cout << "task1 says: " << msg;
}
int main() { 
  std::thread t1(task1, "hello");
  t1.join();
  return 0;
}

Compile and run:

el@defiant ~/foo4/39_threading $ g++ -o s s.cpp      
s.cpp: In function ‘int main()’: 
s.cpp:9:3: error: ‘thread’ is not a member of ‘std’
s.cpp:9:15: error: expected ‘;’ before ‘t1’

You forgot to #include <thread>, include it and try again:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <thread>
using namespace std;
void task1(std::string msg){
  cout << "task1 says: " << msg;
}
int main() { 
  std::thread t1(task1, "hello"); 
  t1.join();
  return 0;
}

Compile and run:

el@defiant ~/foo4/39_threading $ g++ -o s s.cpp -std=c++11
el@defiant ~/foo4/39_threading $ ./s
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)

More errors, as you defined above, because you didn't specify -pthread in the compile:

el@defiant ~/foo4/39_threading $ g++ -o s s.cpp -pthread -std=c++11
el@defiant ~/foo4/39_threading $ ./s
task1 says: hello

Now it works.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335