Outline
Purpose
Provide a thread-enabled adapter for the allocator protocol.
Classes
- See also
- bslma_allocator, bdlma_concurrentmultipool
Description
This component provides an adapter, bdlma::ConcurrentAllocatorAdapter
, that implements the bslma::Allocator
protocol and provides synchronization for operations on an allocator supplied at construction using a mutex also supplied at construction.
,-----------------------------------.
`-----------------------------------'
| ctor/dtor
V
,-----------------.
( bslma::Allocator )
`-----------------'
allocate
deallocate
Definition bdlma_concurrentallocatoradapter.h:288
Thread Safety
bdlma::ConcurrentAllocatorAdapter
is thread-enabled, meaning any operation on the same instance can be safely invoked from any thread.
Usage
This section illustrates intended use of this component.
Example 1: Basic Usage
In the following usage example, we develop a simple AddressBook
class containing two thread-enabled vectors of strings: one for names, the other for addresses. We use a bdlma::ConcurrentAllocatorAdapter
to synchronize memory allocations across our two thread-enabled vectors. For the purpose of this discussion, we first define a simple thread-enabled vector:
template <class TYPE>
class ThreadEnabledVector {
ThreadEnabledVector(const ThreadEnabledVector&);
ThreadEnabledVector& operator=(const ThreadEnabledVector&);
public:
: d_elements(basicAllocator)
{
}
~ThreadEnabledVector() {}
int pushBack(const TYPE& value)
{
return static_cast<int>(d_elements.
size()) - 1;
}
void set(int index, const TYPE& value)
{
d_elements[index] = value;
}
TYPE element(int index) const
{
return d_elements[index];
}
int length() const
{
return static_cast<int>(d_elements.
size());
}
};
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
Definition bslstl_vector.h:1025
void push_back(const VALUE_TYPE &value)
Definition bslstl_vector.h:3760
Definition bslma_allocator.h:457
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:315
We use this thread-enabled vector to create a AddressBook class. However, we use the bdlma::ConcurrentAllocatorAdapter
to prevent our two (thread-enabled) vectors from attempting synchronous memory allocations from our (potentially) non-thread safe bslma::Allocator
. Note that we define a local class, AddressBook_PrivateData
, in order to guarantee that d_allocatorAdapter
and d_mutex
are initialized before the thread-enabled vectors that depend on them:
struct AddressBook_PrivateData {
private:
AddressBook_PrivateData(const AddressBook_PrivateData&);
public:
d_allocatorAdapter;
: d_allocatorAdapter(&d_mutex, basicAllocator)
{
}
};
class AddressBook : private AddressBook_PrivateData {
ThreadEnabledVector<bsl::string> d_names;
ThreadEnabledVector<bsl::string> d_addresses;
private:
AddressBook(const AddressBook&);
public:
: AddressBook_PrivateData(basicAllocator)
, d_names(&d_allocatorAdapter)
, d_addresses(&d_allocatorAdapter)
{
}
~AddressBook()
{
}
{
return d_names.pushBack(name);
}
{
return d_addresses.pushBack(address);
}
{
return d_names.element(index);
}
{
return d_addresses.element(index);
}
int numNames() const
{
return d_names.length();
}
int numAddresses() const
{
return d_addresses.length();
}
};
Definition bslstl_string.h:1281