Thread Safe AllocatorΒΆ
If you want thread-safe access to allocations that come from a particular
umpire::Allocator
, you can create an instance of a
umpire::strategy::ThreadSafeAllocator object that will synchronize access
to it.
In this recipe, we look at creating a umpire::strategy::ThreadSafeAllocator for an umpire::strategy::DynamicPool object:
auto& rm = umpire::ResourceManager::getInstance();
auto pool = rm.makeAllocator<umpire::strategy::DynamicPool>(
"pool", rm.getAllocator("HOST"));
auto thread_safe_pool =
rm.makeAllocator<umpire::strategy::ThreadSafeAllocator>(
"thread_safe_pool", pool);
The complete example is included below:
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-20, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
#include "umpire/strategy/DynamicPool.hpp"
#include "umpire/strategy/ThreadSafeAllocator.hpp"
int main(int, char**)
{
// _sphinx_tag_tut_thread_safe_start
auto& rm = umpire::ResourceManager::getInstance();
auto pool = rm.makeAllocator<umpire::strategy::DynamicPool>(
"pool", rm.getAllocator("HOST"));
auto thread_safe_pool =
rm.makeAllocator<umpire::strategy::ThreadSafeAllocator>(
"thread_safe_pool", pool);
// _sphinx_tag_tut_thread_safe_end
auto allocation = thread_safe_pool.allocate(256);
thread_safe_pool.deallocate(allocation);
return 0;
}