initialization of boost::thread::id object
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 lzw <boostlzw@gmail.com> writes:
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: atomic<boost::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
Thanks a lot for the answer. In fact, I am trying to associate data (std::vector<int> and Widget objects) with a boost::mutex by wrapping its interface [ lock(), try_lock() and unlock() ] as follows: class Widget { ... }; // data to be associated with class MyData { public: MyData(std::vector<int>& v) : v_(v), w_(new Widget) { } ~MyData() { m_.~mutex(); delete w_; } bool is_held() { // test if current thread has locked the thread return id_ == boost::this_thread::get_id(); } std::vector<int>& v() { assert( is_held() ); // lock before accessing data return v_; } Widget* w() { assert( is_held() ); // lock before accessing data return w_; } void lock() { id_ = boost::this_thread::get_id(); m_.lock(); } bool trylock() { bool b = m_.try_lock(); if (b) id_ = boost::this_thread::get_id(); return b; } void unlock() { id_ = 0; // reset it, does not compile m_.unlock(); } private: boost::mutex m_; std::vector<int>& v_; Widget* w_; boost::thread::id id_; }; In the MyData::unlock(), I want to reset id_ to some initial value, but it does not compile. What should I do with it? Thanks in advance, Robert On Tue, Jun 8, 2010 at 12:55 PM, Anthony Williams <anthony.ajw@gmail.com>wrote:
Boost lzw <boostlzw@gmail.com> writes:
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: atomic<boost::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
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost lzw <boostlzw@gmail.com> writes:
Thanks a lot for the answer. In fact, I am trying to associate data (std::vector<int> and Widget objects) with a boost::mutex by wrapping its interface [ lock(), try_lock() and unlock() ] as follows: class Widget { ... }; // data to be associated with class MyData { public: MyData(std::vector<int>& v) : v_(v), w_(new Widget) { }
~MyData() { m_.~mutex();
Do not call destructors manually. This will lead to undefined behaviour.
delete w_; }
void unlock() { id_ = 0; // reset it, does not compile
id_ = boost::thread::id();
m_.unlock(); }
HTH, 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
participants (2)
-
Anthony Williams
-
Boost lzw