diff --git a/base/thread.cpp b/base/thread.cpp index b9ea634..9828b0c 100644 --- a/base/thread.cpp +++ b/base/thread.cpp @@ -6,7 +6,7 @@ namespace base { pthread_key_t Thread::s_key_; Thread::Thread(SocketServer *ss) : start_routine_(NULL), start_pv_(NULL), - MessageQueue(ss) { + started_(false), MessageQueue(ss) { static bool s_first_thread; if (!s_first_thread) { pthread_key_create(&s_key_, NULL); @@ -28,6 +28,7 @@ void Thread::Start(void (start_routine)(void *), void *start_pv) { void *Thread::PreRun(void *pv) { Thread *thread = (Thread *)pv; pthread_setspecific(s_key_, thread); + thread->started_ = true; if (thread->start_routine_ != NULL) { thread->start_routine_(thread->start_pv_); } else { @@ -44,7 +45,7 @@ void Thread::CallerStart(void *pv) { void Thread::Stop(bool wait) { MessageQueue::Stop(); - if (wait && ¤t() != this) { + if (wait && started_ && ¤t() != this) { void *pv; pthread_join(thread_, &pv); } diff --git a/base/thread.h b/base/thread.h index 38f8807..ebacc6a 100644 --- a/base/thread.h +++ b/base/thread.h @@ -50,6 +50,7 @@ private: pthread_t thread_; void (*start_routine_)(void *); void *start_pv_; + bool started_; }; } // namespace base