From a9a9d8927f021effaa31043951f33abb7342a67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Betts?= Date: Sun, 9 Jan 2022 18:04:57 +0100 Subject: [PATCH] Allow threads to be terminated --- bsnes/target-bsnes/bsnes.hpp | 1 + nall/thread.hpp | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/bsnes/target-bsnes/bsnes.hpp b/bsnes/target-bsnes/bsnes.hpp index 1688aedd..6f526bde 100644 --- a/bsnes/target-bsnes/bsnes.hpp +++ b/bsnes/target-bsnes/bsnes.hpp @@ -20,6 +20,7 @@ extern unique_pointer emulator; #include #include #include +#include #include "program/program.hpp" #include "input/input.hpp" diff --git a/nall/thread.hpp b/nall/thread.hpp index fd33e2d5..fd403722 100755 --- a/nall/thread.hpp +++ b/nall/thread.hpp @@ -18,6 +18,7 @@ namespace nall { struct thread { inline auto join() -> void; + inline auto cancel() -> void; static inline auto create(const function& callback, uintptr parameter = 0, uint stacksize = 0) -> thread; static inline auto detach() -> void; @@ -66,6 +67,11 @@ auto thread::exit() -> void { pthread_exit(nullptr); } +auto thread::cancel() -> void { + pthread_cancel(handle); + pthread_join(handle, nullptr); +} + } #elif defined(API_WINDOWS) @@ -75,6 +81,7 @@ namespace nall { struct thread { inline ~thread(); inline auto join() -> void; + inline auto cancel() -> void; static inline auto create(const function& callback, uintptr parameter = 0, uint stacksize = 0) -> thread; static inline auto detach() -> void; @@ -132,6 +139,12 @@ auto thread::exit() -> void { ExitThread(0); } +auto thread::cancel() -> void { + TerminateThread(handle, 0); + WaitForSingleObject(handle, INFINITE); + CloseHandle(handle); + handle = 0; +} } #endif