From 5a53d3681d0f4bd35a8f484e95e3c5e7ae846672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 5 Aug 2017 16:38:24 -0500 Subject: [PATCH] Fixing thread cancellation. --- src/connectivity.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/connectivity.c b/src/connectivity.c index ea13585..0cdf96e 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -118,6 +118,10 @@ int NeoComm_bind(const unsigned short port) { } void *NeoComm_connect_manager() { + // Allow for thread to be cancelled asynchronously + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + while(run) { int cli_sockfd; @@ -181,15 +185,13 @@ void NeoComm_shutdown_directory() { if(!node_list) return; run = 0; - void *res; + pthread_cancel(accept_thread); for(unsigned int i = 0; i < num_nodes; ++i) { - // in case the threads are still running, we wait - pthread_join(node_list[i].thread, &res); + pthread_cancel(node_list[i].thread); close(node_list[i].sockfd); } num_nodes = 0; - pthread_join(accept_thread, &res); close(sockfd); free(node_list);