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