|
Boost : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 2008-05-28 13:10:55
On May 28, 2008, at 6:42 AM, Johan Torp wrote:
> I also expect re-use will mean users have to re-implement
> "unique_thread_id"
> and some really difficult-to-fix bugs for people who do not think
> about it.
> OTOH, I see some value in that std::thread::id are the same as
> window's
> thread ids when you're debugging and post-mortem debugging.
std::thread::id has one bit of functionality that may reduce the need
for "unique_thread_id": It has a notion of an id that is guaranteed
to be "not any thread". For example:
std::thread::id id1;
std::thread::id id2 = std::this_thread::get_id();
assert(id1 != id2);
The assert is guaranteed to pass.
So when you know that a thread is terminating, or is otherwise no
longer valid, one can reset thread id's to not-a-thread:
id2 = std::thread::id();
I've used this (for example) in implementing recursive mutexes:
class recursive_mutex
{
unsigned state_;
std::thread::id id_;
...
public:
recursive_mutex() : id_() ... // id_ guaranteed not to compare
equal to any call to get_id()
...
void unlock()
{
...
if (decrease_lock_count() == 0)
id_ = std::thread::id(); // id_ guaranteed not to
compare equal to any call to get_id()
...
}
...
};
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk