0

我有一个 C++11 程序,它给了我这个错误:

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

代码:

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();
}}

错误是什么意思,我该如何解决?

4

2 回答 2

2

您需要正确链接并编译-std=c++11- 请参阅此示例

我猜你和我有同样的问题!(我用这两个编译-pthread-std=c++11不是链接。(但你需要std=c++11用它编译和链接。))

可能你想做这样的事情:

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

然后

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

……至少我认为是对的。(有人要确认吗?)

于 2013-09-01T17:42:21.120 回答
0

如何重现这些错误:

#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;
}

编译并运行:

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’

你忘了#include <thread>,包括它,然后再试一次:

#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;
}

编译并运行:

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)

更多错误,正如您在上面定义的,因为您没有-pthread在编译中指定:

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

现在它起作用了。

于 2014-02-24T03:31:04.097 回答