MemoryReclaimer.h
// MemoryReclaimer.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2020
#pragma once
#ifndef Ceda_cxMessage_MemoryReclaimer_H
#define Ceda_cxMessage_MemoryReclaimer_H
#include "cxMessage.h"
#include <functional>
namespace ceda
{
/*
Reclaimer
---------
The purpose is to reclaim memory with a delay. It's a thread-safe singleton. We don't care which thread
does the deleting. It can be justified for two reasons:
- Memory deletion is actually expensive, best to offload it so GUI threads etc are more responsive
- Avoid assumptions about mutexes, such as when use ThreadBlockerWhileUsed to delete itself.
*/
cxMessage_API void AsyncReclaimMemory(const std::function<void()>& reclaimFn);
template<typename T>
void AsyncReclaimMemory(T* p)
{
AsyncReclaimMemory( [p]() { delete p; } );
}
} // namespace ceda
#endif // include guard