|
Boost Users : |
Subject: Re: [Boost-users] initialization of boost::thread::id object
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2010-06-08 12:55:28
Boost lzw <boostlzw_at_[hidden]> 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 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