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 5551528..b473d9b 100644
--- a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp
+++ b/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp
@@ -60,10 +60,10 @@
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 @@
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 @@
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) :