Consider a large set of heap allocated objects used to transmit computation result from other threads. Destroying these can be an expensive task.
I am in a scenario where these object are not needed any-more and I just want them to be garbage collected without blocking the main thread. I considered using std::async
the following way to destroy them asynchronously:
#include <memory>
#include <future>
#include <vector>
struct Foo
{
// Complex structure containing heap allocated data.
};
void f() {
std::vector<std::unique_ptr<Foo>> graveyard;
// Process some foo and add them in graveyard.
std::async([fooMoved = std::move(graveyard)]{});
}
In the specific program I am working on, using Visual Studio, profiling seems to confirm that this is faster – with respect to the main thread at least – than just letting graveyard destroy its content at the end of f
scope.
Is this kind of improvement a glitch specific to the conditions of my test or is it a reliable way to deallocate large set of heap data? Do you see any potential drawbacks? Is there a better way to perform a similar task?
Aucun commentaire:
Enregistrer un commentaire