[cpp-threads] Re: Thread API interface tweaks

Alexander Terekhov alexander.terekhov at gmail.com
Thu Aug 31 09:17:07 BST 2006


On 8/31/06, Howard Hinnant <hinnant at twcny.rr.com> wrote:
> I appreciate the free analysis. :-)  Here's my understanding:
>
> On Aug 30, 2006, at 4:20 PM, Peter Dimov wrote:
>
> >> template <class Mutex>
> >> class recursive_mutex
> >> {
> >> private:
> >>     Mutex& mut_;
> >>     unsigned count_;
> >>     thread_id id_;
> >> public:
> >>     explicit recursive_mutex(Mutex& mut)
> >>         : mut_(mut), count_(0) {}  // id_ has "not a thread" value
> >>
> >>     void lock()
> >>     {
> >>         thread_id self = thread_id::current();
> >>         if (id_ == self)
> >
> > Here...
>
> If id_ != self, then obviously this branch isn't taken.  Also no

Suppose that it's even atomic. It's id_ == self that is problematic.
Think of thread id re-use.

[...]
> >>     void unlock()
> >>     {
> >>         if (--count_ == 0)
> >>         {
> >>             id_ = thread_id();  // id_ = "not a thread"
> >
> > ... and here... data race?
>
> It is undefined for a thread for which id_ != self to call unlock().

True, but think about thread id re-use. You need to delay re-use until
id_ = thread_id() becomes globally visible and there's nothing in your
code that ensures it, AFAICS.

regards,
alexander.



More information about the cpp-threads mailing list