Boost logo

Boost Users :

Subject: [Boost-users] initialization of boost::thread::id object
From: Boost lzw (boostlzw_at_[hidden])
Date: 2010-06-08 11:04:10


Hi, I have a class (for illustrative purpose only):

class X {
public:
      X() : id_(0) // does not compile
     { }

     void f() {
           if (operation_failed)
               id_ = 0; // does not compile
          else
               id_ = boost::this_thread::get_id(); // get which thread is
sucessfully executing f()
      }
      bool compare() { // compare current thread with the one that has
sucessfully executed f()
          return id_ == boost::this_thread::get_id();
      }
private:
   boost::thread::id id_;
};

My questions are:

How can I set the id_ to an initial value (e.g., 0 here) for compare()
function to get correct result, assuming multiple threads might be executing
f() and compare() at the same time? Is there a way to do this:
        atomic<boost::thread::id> id_;
to make them thread-safe?

Thanks a lot in advance,
Robert



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net