在多线程程序中使用迭代器循环遍历hash_map
Linux GCC:
我有一个hash_map H,和一个使用迭代器循环H的函数F,问题是函数F可以在多线程模式下执行。我不知道为什么它工作不稳定,似乎迭代器在多线程程序中不安全。有什么想法吗?
MY_HASH::iterator endIter = m_hash.end();
for ( hm_Iter = m_hash.begin( ); hm_Iter != endIter; hm_Iter++)
{
pList->pData[i].id = hm_Iter->second->id ;
pList->pData[i].data = hm_Iter->second->data ;
i++;
}
Linux GCC:
I have a hash_map H, and a function F that using an iterator to loop through H, the problem is that function F can be executed in multithreaded mode. I don't know why it's working not stable, it seems that iterator is not safe in multi-threaded program. Any idea?
MY_HASH::iterator endIter = m_hash.end();
for ( hm_Iter = m_hash.begin( ); hm_Iter != endIter; hm_Iter++)
{
pList->pData[i].id = hm_Iter->second->id ;
pList->pData[i].data = hm_Iter->second->data ;
i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不出哈希迭代会出现问题,但我怀疑分配给
pList->pData[i]
是存在多线程问题的地方。I can't see that the hash iteration would be having problems, but I suspect assigning to
pList->pData[i]
is where your multi-threading issue exists.