开发者

loop through hash_map using iterator in multithreaded program

开发者 https://www.devze.com 2023-03-18 01:50 出处:网络
Linux GCC: I have a hash_map H,开发者_StackOverflow中文版 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 kno

Linux GCC:

I have a hash_map H,开发者_StackOverflow中文版 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++;
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消