[cpp-threads] Re: Thread API interface tweaks

Peter Dimov pdimov at mmltd.net
Wed Aug 30 23:43:28 BST 2006


Howard Hinnant 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
> other thread is capable of setting id_ to self.  Some other thread
> might change id_, but only from one value that is not self to another
> value that is not self.  If id_ == self, then no other thread is
> capable of changing the value of id_ at all.  And in this case, no
> other thread has the ability to change count_ at all.

I'm not saying that there are correctness problems with the test. My point 
is that

    if( id_ == self )

reads id_, and ...

>>>             ++count_;
>>>         else
>>>         {
>>>             mut_.lock();
>>>             id_ = self;

... these ...

>>>             store_release(&count_, 1);
>>>         }
>>>     }
>>>
>>>     void unlock()
>>>     {
>>>         if (--count_ == 0)
>>>         {
>>>             id_ = thread_id();  // id_ = "not a thread"

... two statements write to id_. It seems to me that there is a legitimate 
possibility for the read to overlap one of the writes. Therefore, the type 
of id_ must be atomic. I think. 




More information about the cpp-threads mailing list