CKTIS / C++
std::pmr::unsynchronized_pool_resource — memory pools without synchronization
std::pmr::unsynchronized_pool_resource manages pools of memory blocks for polymorphic allocators. It allows deallocated blocks to be reused, but requires careful resource lifetimes and prevention of simultaneous access from multiple threads.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <memory_resource>
std::pmr::unsynchronized_pool_resource pool;
std::pmr::unsynchronized_pool_resource pool_with_upstream{upstream};
std::pmr::unsynchronized_pool_resource configured_pool{options, upstream};std::pmr::unsynchronized_pool_resource, declared in <memory_resource>, is a memory resource derived from std::pmr::memory_resource. It maintains pools of differently sized blocks. When a pool is exhausted, it obtains another chunk from its upstream resource; requests exceeding the largest supported pool size go directly upstream. Deallocated blocks can serve subsequent allocations.
The default constructor uses std::pmr::get_default_resource(). You can instead supply a pointer to a valid upstream resource and std::pmr::pool_options. The pool does not own its upstream resource: that resource must outlive the pool. options() returns the effective settings, which may differ from those requested; chunk sizes and allocation counts are not portable guarantees.
Do not access the same resource simultaneously from multiple threads. Provide external synchronization or use std::pmr::synchronized_pool_resource. Resource synchronization does not, however, make concurrent modifications of a container using it safe.
release() returns all allocated memory upstream, including blocks not yet returned by clients; destruction does this automatically. It does not replace object destructor calls. Destroy containers and objects using the storage before calling release() or destroying the pool. In the example, the nested scope establishes this order for std::pmr::vector<int>.
Examples
#include <iostream>
#include <memory_resource>
#include <vector>
int main() {
std::pmr::unsynchronized_pool_resource pool;
{
std::pmr::vector<int> values{&pool};
values.reserve(3);
values.push_back(10);
values.push_back(20);
values.push_back(30);
int sum = 0;
for (int value : values) {
sum += value;
}
std::cout << values.size() << ' ' << sum << '\n';
}
pool.release();
}
Result
3 60
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.