我收到未处理的异常和访问冲突读取位置错误。但有时它执行得很完美。我是否正确传递了向量和向量迭代器?可能导致错误的原因是什么。
struct DataStructure
{
MapSmoother *m1;
std::vector<Vertex *> v1;
std::vector<Vertex *>::iterator vit_f;
std::vector<Vertex *>::iterator vit_e;
DataStructure() {
m1 = NULL;
v1;
vit_f;
vit_e;
}
};
DWORD WINAPI thread_fun(void* p)
{
DataStructure *input = (DataStructure*)p;
while ( input->vit_f != input->vit_e ) {
Vertex *v = *((input->vit_f)++);
(*(input->m1)).relax(v);
}
return 0;
}
int main()
{
//Read and encode color to Mesh
// msmoother is an object
DataStructure* input = new DataStructure();
input->m1 = &msmoother;
for(int i = 0; i < 7; ++i) {
for(int color = 1; color <= k; color++) {
std::vector<Vertex *> verList;
//all the vertices with the same color index will be stored in verList vector
input->v1 = verList; //Copied to structure
std::vector<Vertex *>::iterator vit1 = verList.begin();
std::vector<Vertex *>::iterator vit2 = verList.end();
input->vit_f = vit1;
input->vit_e = vit2;
HANDLE hThread[50];
cout << " Processing for color: " << color << endl;
for(int j = 0; j < 50; ++j){
hThread[j] = CreateThread(0,0,(LPTHREAD_START_ROUTINE)&thread_fun,input,0,NULL);
}
WaitForMultipleObjects(THREAD_COUNT,hThread,TRUE,INFINITE);
//clear verList vector
//Close Handles to all threads
}
}
}