2012-12-04 57 views
1

我試圖到正在運行的任務中使用TBB :: concurrent_hash_map掛,但我運行到這個問題,調用地圖的擦除()導致無限鎖的任務。任何想法下面的代碼片段可能是錯誤的?TBB task_group使用concurrent_hash_map時

#include <iostream> 
#include <boost/date_time.hpp> 
#include <tbb/concurrent_hash_map.h> 
#include <tbb/task_group.h> 
#include <tbb/task_scheduler_init.h> 

class BusyTask 
{ 
public:  
    void operator()() { 

     typedef tbb::concurrent_hash_map<unsigned int, int> MyMap; 
     MyMap m; 
     MyMap::accessor a; 
     m.insert(a, 1); 
     m.erase(1); // The task will lock up at this point 

    } 
}; 

int main(int argc, char* argv[]) 
{ 
    std::cout << "Started" << std::endl; 

    BusyTask busyTask1; 

    tbb::task_group taskGroup; 
    taskGroup.run(busyTask1); 
    taskGroup.wait(); 

    std::cout << "Finished" << std::endl; 
    return 0; 
} 

我和TBB V4.0.5測試,GCC 4.7

+1

嘗試將範圍的訪問,例如: {MyMap中::訪問; m.insert(a,1); m.erase(1);} m.erase(1); //任務將鎖定在這個點 – Rick

+0

完美,這是有效的。謝謝! 我應該多一點仔細閱讀了「訪問」文檔:[併發訪問(http://software.intel.com/sites/products/documentation/doclib/tbb_sa/help/reference/containers_overview/concurrent_hash_map_cls/concurrent_access。 HTM) – amoskahiga

回答

1

由於正確的評論指出,這是鎖定範圍的問題。 erase(1)需要獲取已經通過insert()操作獲得的同一個鎖(並且鎖不是遞歸的)。

也請注意擦除(by_accessor)散列圖這保證了完全按訪問保護的元素將被刪除,而不是使用相同的密鑰其他元素的方法。如果併發線程擦除它(提供相同的密鑰)並使用相同的密鑰添加新元素,則後者可能會發生。