Created
March 11, 2022 23:27
-
-
Save jonwis/4e7be4d9495c2ef043670fc0d7ffa64c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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