From: Jake Farrell Date: Thu, 17 May 2012 04:32:10 +0000 (+0000) Subject: Thrift-1606:Race condition in BoostThreadFactory.cpp X-Git-Tag: 0.9.1~365 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=94bb7f26f6d9b5bf1106b1aeb4b983e453f50047;p=common%2Fthrift.git Thrift-1606:Race condition in BoostThreadFactory.cpp Client: cpp Patch: alexandre parenteau Race condition between the line that set state_ to "starting", and the line that checked to make sure that it was "starting". That ended meaning that sometimes calling "start()" would not result in the thread's runnable being called. git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1339477 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp b/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp index 55515282..b473d9bb 100644 --- a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp +++ b/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp @@ -60,10 +60,10 @@ class BoostThread: public Thread { public: BoostThread(bool detached, shared_ptr runnable) : - state_(uninitialized), - detached_(detached) { - this->Thread::runnable(runnable); - } + state_(uninitialized), + detached_(detached) { + this->Thread::runnable(runnable); + } ~BoostThread() { if(!detached_) { @@ -79,27 +79,27 @@ class BoostThread: public Thread { if (state_ != uninitialized) { return; } - - // Create reference + + // Create reference shared_ptr* selfRef = new shared_ptr(); *selfRef = self_.lock(); - thread_ = std::auto_ptr(new boost::thread(boost::bind(threadMain, (void*)selfRef))); + state_ = starting; - if(detached_) - thread_->detach(); + thread_ = std::auto_ptr(new boost::thread(boost::bind(threadMain, (void*)selfRef))); - state_ = starting; + if(detached_) + thread_->detach(); } void join() { if (!detached_ && state_ != uninitialized) { - thread_->join(); + thread_->join(); } } Thread::id_t getId() { - return thread_.get() ? thread_->get_id() : boost::thread::id(); + return thread_.get() ? thread_->get_id() : boost::thread::id(); } shared_ptr runnable() const { return Thread::runnable(); } @@ -163,9 +163,8 @@ class BoostThreadFactory::Impl { void setDetached(bool value) { detached_ = value; } Thread::id_t getCurrentThreadId() const { - return boost::this_thread::get_id(); + return boost::this_thread::get_id(); } - }; BoostThreadFactory::BoostThreadFactory(bool detached) :