Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66146 - trunk/boost/thread
From: anthony_at_[hidden]
Date: 2010-10-22 10:01:15


Author: anthonyw
Date: 2010-10-22 10:01:12 EDT (Fri, 22 Oct 2010)
New Revision: 66146
URL: http://svn.boost.org/trac/boost/changeset/66146

Log:
Fix for issue #4531 --- promise::lazy_init uses shared_ptr atomic access functions to avoid race
Text files modified:
   trunk/boost/thread/future.hpp | 20 ++++++++++++++------
   1 files changed, 14 insertions(+), 6 deletions(-)

Modified: trunk/boost/thread/future.hpp
==============================================================================
--- trunk/boost/thread/future.hpp (original)
+++ trunk/boost/thread/future.hpp 2010-10-22 10:01:12 EDT (Fri, 22 Oct 2010)
@@ -912,10 +912,10 @@
 
         void lazy_init()
         {
- if(!future)
+ if(!atomic_load(&future))
             {
- future_obtained=false;
- future.reset(new detail::future_object<R>);
+ future_ptr blank;
+ atomic_compare_exchange(&future,&blank,future_ptr(new detail::future_object<R>));
             }
         }
         
@@ -945,12 +945,14 @@
             future_obtained(rhs.future_obtained)
         {
             future.swap(rhs.future);
+ rhs.future_obtained=false;
         }
         promise & operator=(promise&& rhs)
         {
             future.swap(rhs.future);
             future_obtained=rhs.future_obtained;
             rhs.future.reset();
+ rhs.future_obtained=false;
             return *this;
         }
 #else
@@ -958,12 +960,14 @@
             future(rhs->future),future_obtained(rhs->future_obtained)
         {
             rhs->future.reset();
+ rhs->future_obtained=false;
         }
         promise & operator=(boost::detail::thread_move_t<promise> rhs)
         {
             future=rhs->future;
             future_obtained=rhs->future_obtained;
             rhs->future.reset();
+ rhs->future_obtained=false;
             return *this;
         }
 
@@ -1047,10 +1051,10 @@
 
         void lazy_init()
         {
- if(!future)
+ if(!atomic_load(&future))
             {
- future_obtained=false;
- future.reset(new detail::future_object<void>);
+ future_ptr blank;
+ atomic_compare_exchange(&future,&blank,future_ptr(new detail::future_object<void>));
             }
         }
     public:
@@ -1079,12 +1083,14 @@
             future_obtained(rhs.future_obtained)
         {
             future.swap(rhs.future);
+ rhs.future_obtained=false;
         }
         promise & operator=(promise&& rhs)
         {
             future.swap(rhs.future);
             future_obtained=rhs.future_obtained;
             rhs.future.reset();
+ rhs.future_obtained=false;
             return *this;
         }
 #else
@@ -1092,12 +1098,14 @@
             future(rhs->future),future_obtained(rhs->future_obtained)
         {
             rhs->future.reset();
+ rhs->future_obtained=false;
         }
         promise & operator=(boost::detail::thread_move_t<promise> rhs)
         {
             future=rhs->future;
             future_obtained=rhs->future_obtained;
             rhs->future.reset();
+ rhs->future_obtained=false;
             return *this;
         }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk