2013-09-01 89 views
0

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

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

+0

可能重复[C++线程,std :: system \ _error - 操作不允许?](http://stackoverflow.com/questions/17274032/c-threads-stdsystem-error-operation-not- permitted) – user3728501

+0

通常,当你忘记'-pthread'标志时会发生这些错误。 –

回答

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

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

+0

我在NetBeans IDE中执行此操作。另一个类似的代码工作正常我正在使用C++ 11(在NetBeans中选择了该选项) – George

+0

@George这次它链接正确吗?我认为你可以在netbeans中设置一个链接器标志? – user3728501

+0

我不必在另一个类似的程序中明确使用-pthread标志。但现在也试过了。同样的结果。 – George

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

编译并运行:

[email protected] ~/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; 
} 

编译并运行:

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

更多的错误,因为你上面定义的,因为你没有在编译指定-pthread

[email protected] ~/foo4/39_threading $ g++ -o s s.cpp -pthread -std=c++11 
[email protected] ~/foo4/39_threading $ ./s 
task1 says: hello 

现在,它的工作原理。