2013-04-10 73 views
2

最近我從系統內存管理(malloc/free)切換到英特爾的TBB可擴展分配器。問題是如果它是線程安全的,我找不到任何信息。整個TBB是圍繞線程構建的,所以它看起來合乎邏輯,但沒有具體的證據,我不想承擔這樣的事情。但是,我不想做任何不必要的同步。有沒有人有關於此的一些信息?線程安全的TBB可擴展分配器

+1

這絕對是線程安全的。必須在文檔的某處。 – 2013-04-10 18:48:10

+0

http://software.intel.com/sites/products/documentation/doclib/tbb_sa/help/reference/memory_allocation/scalable_allocator_cls/c_interface_to_scalable_allocator.htm - 這是所有可用文檔。你絕對確定它是線程安全的嗎? – krojew 2013-04-10 18:50:29

回答

2

按照你鏈接的手冊:

除非另有說明,該庫的線程安全規則 如下:

兩個線程可以調用不同 對象的方法或功能的同時,但不是同一個對象。對於兩個線程 同時調用同一對象上的方法或函數是不安全的。 類別的說明註釋偏離本公約。例如,對於 示例,併發容器比較寬鬆。就其性質而言,它們確實允許在同一容器 對象上進行一些併發操作。

對於可擴展的分配器,這意味着兩個線程不能同時釋放相同的內存,這應該不會令人意外。

+0

這個引用有些含糊。可伸縮的分配器C API不明確地涉及任何正在傳遞的對象/結構體,但可以在內部進行或不進行同步。因此,不清楚(至少對我來說)在不同的線程中使用它是安全的還是每個調用都應該包含在互斥鎖中。 – krojew 2013-04-10 19:03:33

+1

過度複雜的API:alloc函數沒有對象,自由函數有一個。 – 2013-04-10 19:05:34

+0

經過一番挖掘,我發現http://software.intel.com/zh-cn/blogs/2011/12/19/scalable-memory-pools-community-preview-feature有關線程安全的信息,它看起來像與你說的一致。謝謝。 – krojew 2013-04-10 19:13:05

4

那麼這個源Intel Threading Building Blocks聽起來更直(見頁「TBB可擴展分配器」) -

每個線程都有自己的私有堆

- 大小 - 隔離箱提高當地
- 專用堆減少 同步開銷和虛假共享

更新:從here -

TBB提供了一種可擴展的分配器與每個線程池。 它可能仍有錯誤分享。

example: false sharing could matter in pipelining. 

    TBB also provides a cache-aligned allocator, which guarantees 
    that any two things you've allocated will never experience 
    false sharing. The downside is that it has larger memory 
    pressure. This is accomplished by making the minimum allocation 
    N cache lines, where N is a small integer. 

    In the book, the conventional wisdom is to start with the 
    scalable allocator and see if switching to the cache-aligned 
    allocator speeds things up. 

但共享假即將放緩不是線程安全的。