C ++ 11线程错误

时间:2013-09-01 17:40:02

标签: multithreading c++11

我有一个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();
}}

错误意味着什么,我该如何解决?

2 个答案:

答案 0 :(得分:2)

您需要正确链接,并使用-std=c++11进行编译 - 请参阅此example

我猜你和我有同样的问题! (我使用-pthread-std=c++11而不是链接与这两个进行编译。(但您需要使用std=c++11进行编译以及与之链接。) )

可能你想做这样的事情:

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

然后

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

......至少我认为这是对的。 (有人确认?)

答案 1 :(得分: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

现在可行。