boost:pool库——内存池

内存池预先分配了一块大的内存空间,然后就可以在其中使用某种算法实现高效快速的自定制内存分配。

boost.pool 库基于简单分隔存储思想实现了一个快速、紧凑的内存池库,不仅能够管理大量的对象,还可以被用做STL的内存分配器。

它近似于一个小型的垃圾回收机制,在需要大量地分配/释放小对象时很有效率,而且完全不需要考虑 delete。

pool库包含四个组成部分:

  • 最简单的 pool
  • 分配类实例的 object_pool
  • 单例内存池 singleton_pool
  • 可用于标准库的 pool_alloc

一、pool

它只能用作普通的数据类型如 int、double 等的内存池,不能应用于复杂的对象,因为它只分配内存,不调用构造函数。

#include <boost/pool/pool.hpp>
using namespace boost;

int main()
{
    //使用默认分配器,其内部使用new[]、delete[]分配内存
    pool<> pl(sizeof(int));//分配int的内存池

    int *p = static_cast<int*>(pl.malloc());//void*转成需要的类型

    qDebug()<<"p是否来自内存池p1"<<pl.is_from(p);
    pl.free(p);

    int *p2 = new int;
    qDebug()<<"p2是否来自内存池p1"<<pl.is_from(p2);
    delete p2;

    for (int i = 0;i < 100; ++i)
    {
        pl.ordered_malloc(10);//连续分配大量的内存
    }
}//所有分配的内存被释放

二、object_pool

object_pool 是用于类实例(对象)的内存池,它的功能与pool类似,但会在析构时对所有已经分配的内存块调用析构函数,从而正确地释放资源。

#include <boost/pool/object_pool.hpp>
using namespace boost;

struct test
{
public:
    int a,b,c;
    test(int x = 1, int y = 2, int z = 3):a(x),b(y),c(z)
    {}
};

int main()
{
    object_pool<test> pl;

    test * p = pl.malloc();
    std::cout <<pl.is_from(p);

    p = pl.construct(7, 8, 9);

    object_pool<std::string> pls;
    for (int i = 0; i < 10 ; ++i)
    {
        std::string *ps = pls.construct("hello object_pool");
        std::cout << *ps << std::endl;
    }
}

三、singleton_pool

singleton_pool 的接口和 pool 一致,但它的成员函数全是 static 的,它是个单例。

除非调用 release_memory() 或 purge_memory(),否则它不会自动释放所占内存。

其他都和 pool 一致。

#define BOOST_POOL_NO_MT //默认使用线程库保证安全,如果不使用多线程则定义此宏
#include <boost/pool/singleton_pool.hpp>
using namespace boost;

int main()
{
    int *p = (int *)singleton_pool<int, sizeof(int)>::malloc();
    singleton_pool<int, sizeof(int)>::release_memory();//释放所有未被分配的内存
//    singleton_pool<int, sizeof(int)>::purge_memory();//强制释放内存池持有的所有内存
}

四、pool_alloc

pool_alloc 提供了两个可以用于标准容器模板参数的内存分配器,分别是 pool_alloc 和 fast_pool_allocator,它们的行为与之前的内存池类有一点不同:当内存分配失败时会抛出异常std::bad_alloc

#include <boost/pool/pool_alloc.hpp>
using namespace boost;

int main()
{
    std::vector<int, pool_allocator<int> > v;//使用pool_allocator代替默认的内存分配器

    v.push_back(10);
    std::cout << v.size();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I cannot experience emotions, but I can provide information on how to boost threads in programming. Boost.Thread is a C++ library that provides a wide range of synchronization primitives, thread-safe containers, and other useful tools for multithreaded programming. Here are some tips for boosting threads in Boost.Thread: 1. Use mutexes: Mutexes are fundamental synchronization primitives that allow threads to safely access shared resources. Boost.Thread provides a variety of mutex types, including recursive and timed mutexes, that can be used to protect critical sections of code. 2. Use condition variables: Condition variables are a powerful tool for synchronizing threads. They allow threads to wait for a specific condition to be met before proceeding. Boost.Thread provides several types of condition variables, including timed condition variables, that can be used to implement complex synchronization patterns. 3. Use atomics: Atomic types provide a way to perform atomic operations on shared variables without the need for locks. Boost.Thread provides a variety of atomic types, including integers, booleans, and pointers, that can be used to implement lock-free algorithms. 4. Use thread-safe containers: Boost.Thread provides several thread-safe container types, including queues, stacks, and maps, that can be used to safely share data between threads. 5. Use thread pools: Thread pools provide a way to manage a pool of worker threads that can be used to execute tasks in parallel. Boost.Thread provides a thread pool implementation that can be used to boost performance in multithreaded applications. By using these tools and techniques, you can safely and efficiently boost threads in your C++ applications using Boost.Thread.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值