[cpp-threads] Re: Thread API interface tweaks
Howard Hinnant
hinnant at twcny.rr.com
Wed Aug 30 23:53:27 BST 2006
On Aug 30, 2006, at 6:43 PM, Peter Dimov wrote:
> 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.
<chuckle> I thought I had enough memory barriers in there to prevent
that (I'm assuming the mutex does the proper acquire/release dance).
But last week Herb dove into the theory of relativity to describe
this painfully delicate part of my reality. So I'm just going to sit
behind that big rock over there for awhile and see what blows by... :-)
-Howard
More information about the cpp-threads
mailing list