I'm trying to make a custom allocator/deallocator thread safe with the following:
std::mutex myMutex; //declared globally
...
T* allocate(std::size_t n)
{
std::lock_guard<std::mutex> guard(myMutex);
...pointer incrementing via singleton class
...each call should return successive locations
...so different containers are allotted contiguously
}
void deallocate(T* p, std::size_t n) noexcept
{
std::lock_guard<std::mutex> guard(myMutex);
...
}
However my output with these allocators, allocating containers on different threads, is no different from when I allocate with non-locking allocators, which it should not be if the allocator and deallocator are locked. In particular each thread allocates to the first memory location that was available when all the threads were launched. They clearly don't 'see' the adjustments made by the other threads. What's wrong with the above?
Aucun commentaire:
Enregistrer un commentaire