
Boost lzw
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
The default constructor for thread::id yields a "not any thread" value.
, assuming multiple threads might be executing f() and compare() at the same time? Is there a way to do this: atomicboost::thread::id id_; to make them thread-safe?
If you want to protect a value for safe access from multiple threads, use a mutex. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976