簡體   English   中英

boost :: singleton_pool中的對象創建

[英]Object creation in boost::singleton_pool

我試圖使用boost :: singleton_pool在高性能的關鍵多線程應用程序中創建大量“Order”類型的對象。 看一下文檔,這就是我應該做的,

struct OrderTag{};
typedef boost::singleton_pool<OrderTag, sizeof(Order)> OrderPool; 

boost :: singleton_pool有一個靜態成員函數malloc,它返回void *的指針,但是我需要通過調用它的構造函數在OrderPool中創建Order類型的對象。 我應該使用boost :: pool_allocator和singleton_pool這樣做嗎?

謝謝。

簡而言之:沒有。 boost :: pool_allocator實現本身使用boost :: singleton_pool並提供類似std :: allocator的接口,因此您可以將它與STL容器(但不僅僅是STL而不僅僅是容器)一起使用,例如vector,list等.UserAllocator概念不是像boost :: pool_allocator但是它控制着最低級別的內存管理。 例如,我編寫了UserAllocator,它通過mmap()函數分配內存,而根本不使用堆。

因此,要創建“Order”類型的對象,您應該使用boost :: pool_allocatorboost :: fast_pool_allocator 在您的情況下,沒有必要直接使用boost :: singleton_pool。

您可以將singleton_pool與placement new一起使用,如下所示:

Order* p = new (OrderPool.malloc()) Order(/* add ctor parameters if any*/);

或者使用boost::object_pool<Order>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM