我收到未处理的异常和访问冲突读取位置错误。但有时它会完美地执行。我是否正确地传递了向量和向量迭代器?可能导致错误的原因是什么?
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
}
}
}

发布于 2013-02-05 09:20:42
不,你没有正确地传递它们。
这段代码:
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;这就是问题所在。实际上,您在input对象中设置的迭代器来自本地verList;而不是您的input->v1列表。
尝试:
input->vit_f = input->v1.begin();
input->vit_e = input->v1.end();附录
已为OP更新,以避免竞争条件和未成功的向量复制。
首先,将DataStructure重新定义为:
struct DataStructure
{
MapSmoother *m1;
std::vector<Vertex *>::iterator first, last
DataStructure() : m1(NULL) {}
};接下来,将线程函数定义为:
DWORD WINAPI thread_fun(void* p)
{
DataStructure *input = static_cast<DataStructure*>(p);
if (input && input->m1)
{
for (std::vector<Vertex *>::iterator it = input->first
it != input->last; ++it)
{
input->m1->relax(*it);
}
}
delete input;
return 0;
}最后,像这样调用它:(在你的循环中)
// all the vertices with the same color index will be stored in verList vector
std::vector<Vertex *> verList;
// ... store vector data ...
cout << " Processing for color: " << color << endl;
HANDLE hThread[50] = {NULL};
for(int j = 0; j < _countof(hThread); ++j)
{
DataStructure *input= new DataStructure;
input->first = verList.begin();
input->last = verList.end();
input->m1 = &msmoother
hThread[j] = CreateThread(0,0,(LPTHREAD_START_ROUTINE)&thread_fun,input,0,NULL);
}
WaitForMultipleObjects(THREAD_COUNT,hThread,TRUE,INFINITE);或者类似的东西。我只是在网上敲了敲,所以我不知道它是否编译,但希望能给你一个足够好的想法。每个线程在verList数组中都有自己的一对迭代器,因此可以进行并发访问。注意:他们不能修改向量,只能使用它。他们获取迭代器的结构是由创建者线程分配的,但属于线程本身,而线程本身最终要对销毁它负责。
发布于 2013-02-05 09:19:50
我是一个C++新手,所以我可能错了,但我认为问题可能出在这里:
std::vector<Vertex *>::iterator vit1 = verList.begin();
std::vector<Vertex *>::iterator vit2 = verList.end();
input->vit_f = vit1;
input->vit_e = vit2;您将迭代器传递给verList,而不是传递给已复制到DataStructure的向量。我想你想要的是:
input->v1 = verList; // copy to structure
input->vit_f = input->v1.begin();
input->vit_e = input->v1.end();https://stackoverflow.com/questions/14698371
复制相似问题