[Thread]: code snippet simulating future

Hi folks, To better understand the "future" concept, I am looking at the following code and have several questions: (1) I got a compiling warning: 'this' : used in base member initializer list. How can I get rid of it? (2) how can I bind class's local data x_ into the new thread? I tried: t_(boost::bind(&future<T>::run, this, x_) ), but it does not work. (3) There seems a race condition in the overloaded function call operator()(). What wrong did I do? BTW: if the future is constructed as ctor using: future(boost::function<T ()> const& f); the code snippet works with minor modification. Thanks in advance, Cheers, Robert ====== code snippet ===== #include <boost/bind.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/thread.hpp> #include <boost/function.hpp> template<typename T> class future : boost::noncopyable { public: future(boost::function<T (int)> const& f, int); T operator()(); private: int x_; bool joined_; T v_; boost::function<T (int)> const& f_; boost::mutex m_; void run(); boost::thread t_; }; template<typename T> future<T>::future(boost::function<T (int)>const& f, int x) : x_(x), joined_(false), v_(T()), f_(f), t_(boost::bind(&future<T>::run, this) ) { } template<typename T> T future<T>::operator()() { mutex::scoped_lock lock(m_); if (!joined_) { t_.join(); joined_ = true; } return v_; } template<typename T> void future<T>::run() { v_ = f_(x_); }
participants (1)
-
Boost lzw