Skip to content

Instantly share code, notes, and snippets.

@fredemmott
Last active April 12, 2022 13:38
Show Gist options
  • Save fredemmott/0a593248d48aaae8fb2c933e61c11d8d to your computer and use it in GitHub Desktop.
Save fredemmott/0a593248d48aaae8fb2c933e61c11d8d to your computer and use it in GitHub Desktop.
#include <chrono>
#include <thread>
#include <Windows.h>
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
{
std::jthread a([]() {
std::this_thread::sleep_for(std::chrono::seconds(1));
if (HeapLock(GetProcessHeap())) {
OutputDebugStringA("Locked heap in A\n");
}
std::this_thread::sleep_for(std::chrono::seconds(10));
OutputDebugStringA("About to unlock heap in A\n");
if (HeapUnlock(GetProcessHeap())) {
OutputDebugStringA("Unlocked heap in A\n");
} else {
OutputDebugStringA("Failed to unlock heap in A\n");
}
});
std::jthread b([]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
if (HeapUnlock(GetProcessHeap())) {
OutputDebugStringA("Unlocked in B\n");
} else {
OutputDebugStringA("Failed to unlock in B\n");
}
if (HeapLock(GetProcessHeap())) {
OutputDebugStringA("Locked in B\n");
HeapUnlock(GetProcessHeap());
}
});
std::jthread c([]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
HeapLock(GetProcessHeap());
OutputDebugStringA("Locked in C\n");
HeapUnlock(GetProcessHeap());
});
}
OutputDebugStringA("Finished all threads\n");
return 0;
}
Locked heap in A
Unlocked in B
Locked in B
Locked in C
About to unlock heap in A
Unlocked heap in A
Finished all threads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment