From 7ea924c76314d86d3191889b04477805b84f1005 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Thu, 8 Feb 2018 19:12:28 +0100 Subject: [PATCH] Problem: segfault on thread_t::stop if thread was never started Solution: add started flag --- src/thread.cpp | 18 ++++++++++++------ src/thread.hpp | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index 65881042..bce24630 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -59,14 +59,17 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_) (HANDLE) _beginthreadex (NULL, 0, &::thread_routine, this, 0, NULL); #endif win_assert (descriptor != NULL); + started = true; } void zmq::thread_t::stop () { - DWORD rc = WaitForSingleObject (descriptor, INFINITE); - win_assert (rc != WAIT_FAILED); - BOOL rc2 = CloseHandle (descriptor); - win_assert (rc2 != 0); + if (started) { + DWORD rc = WaitForSingleObject (descriptor, INFINITE); + win_assert (rc != WAIT_FAILED); + BOOL rc2 = CloseHandle (descriptor); + win_assert (rc2 != 0); + } } void zmq::thread_t::setSchedulingParameters ( @@ -116,12 +119,15 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_) arg = arg_; int rc = pthread_create (&descriptor, NULL, thread_routine, this); posix_assert (rc); + started = true; } void zmq::thread_t::stop () { - int rc = pthread_join (descriptor, NULL); - posix_assert (rc); + if (started) { + int rc = pthread_join (descriptor, NULL); + posix_assert (rc); + } } void zmq::thread_t::setSchedulingParameters ( diff --git a/src/thread.hpp b/src/thread.hpp index 8d7329ab..2442975e 100644 --- a/src/thread.hpp +++ b/src/thread.hpp @@ -52,6 +52,7 @@ class thread_t inline thread_t () : tfn (NULL), arg (NULL), + started (false), thread_priority (ZMQ_THREAD_PRIORITY_DFLT), thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT) { @@ -81,6 +82,8 @@ class thread_t void *arg; private: + bool started; + #ifdef ZMQ_HAVE_WINDOWS HANDLE descriptor; #else