Fix crash caused by trying to stop a thread that hasn't started.

This commit is contained in:
Scott Ludwig 2016-06-04 19:00:50 -07:00 committed by Nathan Fulton
parent 6a0c49d05e
commit b91e6e0b4b
2 changed files with 4 additions and 2 deletions

View File

@ -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 && &current() != this) {
if (wait && started_ && &current() != this) {
void *pv;
pthread_join(thread_, &pv);
}

View File

@ -50,6 +50,7 @@ private:
pthread_t thread_;
void (*start_routine_)(void *);
void *start_pv_;
bool started_;
};
} // namespace base