Skip to content

Instantly share code, notes, and snippets.

@jonwis
Created March 11, 2022 23:27
Show Gist options
  • Save jonwis/4e7be4d9495c2ef043670fc0d7ffa64c to your computer and use it in GitHub Desktop.
Save jonwis/4e7be4d9495c2ef043670fc0d7ffa64c to your computer and use it in GitHub Desktop.
#include <wil/resource.h>
template<typename Q> bool SubmitThreadpoolLambda(Q&& fn)
{
using base_type = std::decay_t<Q>;
struct ContextObject : base_type
{
ContextObject(Q&& q) : base_type(std::move(q)) {}
static void NTAPI Callback(PTP_CALLBACK_INSTANCE, PVOID context)
{
std::unique_ptr<ContextObject> self(reinterpret_cast<ContextObject*>(context));
(*self)();
}
};
auto co = std::make_unique<ContextObject>(std::forward<Q>(fn));
if (::TrySubmitThreadpoolCallback(&ContextObject::Callback, co.get(), nullptr))
{
co.release();
return true;
}
else
{
return false;
}
}
void use()
{
wil::slim_event evt;
SubmitThreadpoolLambda([&evt]() {
evt.SetEvent();
});
evt.wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment