Permalink
Browse files

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
  • Loading branch information...
1 parent e8c259a commit 94bb7f26f6d9b5bf1106b1aeb4b983e453f50047 @jfarrell jfarrell committed May 17, 2012
Showing with 13 additions and 14 deletions.
  1. +13 −14 lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp
View
27 lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp
@@ -60,10 +60,10 @@ class BoostThread: public Thread {
public:
BoostThread(bool detached, shared_ptr<Runnable> 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<BoostThread>* selfRef = new shared_ptr<BoostThread>();
*selfRef = self_.lock();
- thread_ = std::auto_ptr<boost::thread>(new boost::thread(boost::bind(threadMain, (void*)selfRef)));
+ state_ = starting;
- if(detached_)
- thread_->detach();
+ thread_ = std::auto_ptr<boost::thread>(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> 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) :

0 comments on commit 94bb7f2

Please sign in to comment.