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-19, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include "umpire/strategy/DynamicPool.hpp"
#include "umpire/strategy/ThreadSafeAllocator.hpp"

#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"

int main(int, char**) {
  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);

  auto allocation = thread_safe_pool.allocate(256);
  thread_safe_pool.deallocate(allocation);

  return 0;
}