Skip to content

Instantly share code, notes, and snippets.

@SpaceManiac
Last active August 18, 2018 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpaceManiac/b23ec263e786c09dc89f0399b97e71fc to your computer and use it in GitHub Desktop.
Save SpaceManiac/b23ec263e786c09dc89f0399b97e71fc to your computer and use it in GitHub Desktop.
Demo of thread reclamation crashes w/o hooking world/Del
#include <thread>
#include <memory>
#include <atomic>
using namespace std;
using namespace std::chrono_literals;
atomic<bool> should_quit;
struct wrapper {
thread th;
wrapper(thread&& th) : th(move(th)) {}
~wrapper() { should_quit.store(true); th.join(); }
};
unique_ptr<wrapper> handle;
atomic<int> message;
extern "C" const char* start() {
handle = make_unique<wrapper>(thread([](){
for (int i = 1; i < 10; ++i) {
message.store(i);
this_thread::sleep_for(1s);
if (should_quit.load()) {
return;
}
}
message.store(-1);
}));
return "";
}
extern "C" const char* update() {
static char buf[128];
int msg = message.load();
if (msg == 0) {
return "...";
} else if (msg == -1) {
return "";
} else {
message.store(0);
sprintf(buf, "%d", msg);
return buf;
}
}
extern "C" const char* stop() {
handle.reset();
return "";
}
#define DLL @"test.dll"
/client/verb/Go()
src << "starting"
call(DLL, "start")()
var/r
while ((r = call(DLL, "update")()))
if (r != "...")
src << "progress: [r]"
sleep (1)
src << "done"
/client/verb/Reboot_Badly()
world.Reboot()
/client/verb/Reboot_Safely()
call(DLL, "stop")()
world.Reboot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment