我正在編寫一個程序,其中多個線程將條目添加到(tbb :: concurrent_hash_map)哈希映射中,同時其他線程遍歷該映射並操作哈希映射上的條目。在每次一個線程操縱並使用一個入口並使用訪問者阻止該入口(以便不會有衝突並且其他線程無法訪問該入口)。操作數據後,線程插入數據並釋放訪問器。tbb :: concurrent_hash_map - 如果鍵入被其他線程阻塞,如何返回
我現在的問題是,一個線程如何訪問被阻止的條目,直到該條目的訪問者被釋放,即使哈希映射中的其他條目未被阻止。我想要實現的目標是,線程跳過被阻止的條目並轉到下一個非阻止條目或返回到我的函數。有沒有很好的解決方案?
下面的代碼片段短單線程例如:
...
typdef tbb::concurrent_hash_map<int,unsigned int> typ_hash_map;
typ_hash_map hash_map;
typ_hash_map::accessor acc;
typ_hash_map::accessor acc2;
//hash_map filled with 4 entries...
//block second entry in hash map
typ_hash_map::iterator k = hash_map.begin();
k++;
config.hash_map.insert(acc,k->first);
//travers all entries in hash_map
for(typ_hash_map::iterator j = hash_map.begin();j!=hash_map.end(); j++){
hash_map.find(acc2,j->first); // my problem: return if entry is blocked - at the moment its waiting till acc is released
/*
Do something with acc2->second if entry is not blocked,
else continue;
*/
}
...