boost pool_allocator 内存池行为说明
boost pool_allocator memory pool behavior clarification
boost文档如下:
Note
The underlying singleton_pool
used by the this allocator constructs a pool instance that is never freed. This means that memory allocated by the allocator can be still used after main()
has completed, but may mean that some memory checking programs will complain about leaks.
我很困惑,因为我检查了代码,singleton_pool
似乎仍然只在当前进程的堆上创建。 IE。如果进程被 OS 杀死,这样的池是否会被释放?那么上面的注释仅仅意味着如果一些守护线程继续运行并且这样的池在 main() 之后仍然可用?或者它实际上意味着即使整个进程被杀死也不会释放池?
在我看来,pool_allocator
和 fast_pool_allocator
都使用相同的机制来分配内存,即从这样的 singleton_pool
单例中分配内存。但是,此注释并未针对 fast_pool_allocator
指定。对于上面的注释,我认为它们的行为相同。我说得对吗?
请帮忙。谢谢。
singleton_pool
实现thread-safe(在某些情况下)无锁单例,使用non-local 静态变量的初始化特性。源码部分:
template <typename Tag,
unsigned RequestedSize,
typename UserAllocator,
typename Mutex,
unsigned NextSize,
unsigned MaxSize >
class singleton_pool
{
...
struct object_creator
{
object_creator()
{ // This constructor does nothing more than ensure that instance()
// is called before main() begins, thus creating the static
// T object before multithreading race issues can come up.
singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::get_pool();
}
inline void do_nothing() const
{
}
};
static object_creator create_object;
}; // struct singleton_pool
non-local 变量在程序启动时初始化,在 main 函数开始执行之前,并在程序终止时被拆除。这样 singleton_pool
将在 main()
之前创建,并在 main()
之后销毁。如果进程无论如何终止,当然 pool 将被释放。
boost文档如下:
Note
The underlying
singleton_pool
used by the this allocator constructs a pool instance that is never freed. This means that memory allocated by the allocator can be still used aftermain()
has completed, but may mean that some memory checking programs will complain about leaks.
我很困惑,因为我检查了代码,singleton_pool
似乎仍然只在当前进程的堆上创建。 IE。如果进程被 OS 杀死,这样的池是否会被释放?那么上面的注释仅仅意味着如果一些守护线程继续运行并且这样的池在 main() 之后仍然可用?或者它实际上意味着即使整个进程被杀死也不会释放池?
在我看来,pool_allocator
和 fast_pool_allocator
都使用相同的机制来分配内存,即从这样的 singleton_pool
单例中分配内存。但是,此注释并未针对 fast_pool_allocator
指定。对于上面的注释,我认为它们的行为相同。我说得对吗?
请帮忙。谢谢。
singleton_pool
实现thread-safe(在某些情况下)无锁单例,使用non-local 静态变量的初始化特性。源码部分:
template <typename Tag,
unsigned RequestedSize,
typename UserAllocator,
typename Mutex,
unsigned NextSize,
unsigned MaxSize >
class singleton_pool
{
...
struct object_creator
{
object_creator()
{ // This constructor does nothing more than ensure that instance()
// is called before main() begins, thus creating the static
// T object before multithreading race issues can come up.
singleton_pool<Tag, RequestedSize, UserAllocator, Mutex, NextSize, MaxSize>::get_pool();
}
inline void do_nothing() const
{
}
};
static object_creator create_object;
}; // struct singleton_pool
non-local 变量在程序启动时初始化,在 main 函数开始执行之前,并在程序终止时被拆除。这样 singleton_pool
将在 main()
之前创建,并在 main()
之后销毁。如果进程无论如何终止,当然 pool 将被释放。