Boost logo

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