From 274bda43a81c08a2a521751f6b4dffba3d4e24e1 Mon Sep 17 00:00:00 2001 From: Tom Tsou Date: Fri, 14 Nov 2014 15:06:21 -0800 Subject: [PATCH] CommonLibs: Add thread cancellation capability For clean shutdown in the transceiver we need to cancel and join running threads for orderly unwinding. Thread cancellation points already exist, so we just need to be able to call on the threads to exit out when stopping or shutting down. Don't error when joining a NULL thread, which would be the case if a thread was stopped before ever being started to begin with. Signed-off-by: Tom Tsou --- CommonLibs/Threads.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CommonLibs/Threads.h b/CommonLibs/Threads.h index 6d3e959..0cbe50a 100644 --- a/CommonLibs/Threads.h +++ b/CommonLibs/Threads.h @@ -162,8 +162,15 @@ class Thread { void start(void *(*task)(void*), void *arg); /** Join a thread that will stop on its own. */ - void join() { int s = pthread_join(mThread,NULL); assert(!s); } + void join() { + if (mThread) { + int s = pthread_join(mThread, NULL); + assert(!s); + } + } + /** Send cancelation to thread */ + void cancel() { pthread_cancel(mThread); } };