|
Boost : |
From: Dirk Gerrits (dirkg_at_[hidden])
Date: 2002-05-16 14:38:06
----- Original Message -----
From: "William E. Kempf" <williamkempf_at_[hidden]>
> > I'm not 100% sure exactly which issues you mean. Surely, the
> > issue that auto_ptrs can't be used in containers is not
> > relevant here. ;)
>
> It's not relevant for auto_ptrs either. Seriously, that's precisely the
> problem I'm referring to (though there are cases other then just adding
them
> to containers that have the same issues... you illustrate one below).
Forgive my threading newbieness (that's probably not a word ;) but why would
you want to store locks in a container?
> > But 'pass by value acts like a sink' does apply here right?
> >
> > void func(auto_ptr ptr) {};
> > {
> > // code here
> > } // ptr's value is destroyed [mutex is unlocked]
> >
> > int main()
> > {
> > auto_ptr<int> ptr(new int(0));
> > // use ptr here, no problemo [use locked object]
> >
> > func(ptr);
> >
> > // use ptr again (as a null pointer!) [use object, now unlocked!]
> >
> > } // destructor is now a no-op
> >
> > I can see this would be problematic. But what other issues are
> > you referring to?
>
> You're mixing "metaphors" here. The code shows only an auto_ptr, while
your
> comments refer to lock states. So I can't be 100% sure what you mean
here,
> though you have illustrated the issue I was referring to above (the same
one
> as there is for containers, actually). If "ptr" were instead a ScopedLock
> then after the call to func() (assuming it was pass by value) the lock
> object would no longer be valid (this is stronger then just the object
being
> unlocked). If you know the rules and obey them this is not an issue...
but
> we know how that works in the real world.
Sorry about the metaphors, I wasn't really trying to be a poet. :P I'll be
more
specific:
// Proper includes here
void func(NewMutex::destructive_copy_lock lock)
{
// function now 'owns' the IO lock, probably not a smart idea!
std::cout << "Hello world!" << std::endl;
}; // unlock (and as you say, invalidate the lock object)
NewMutex iomutex;
void threadFunc()
{
NewMutex::destructive_copy_lock lock(iomutex);
std::cout << "Yo, I'm a thread!" << std::endl;
func(lock);
std::cout << "This is not good, right?" << std::endl:
}; // what's up with ~destructive_copy_lock() ?
// main function with several threads here
Is this what you were thinking about?
Dirk Gerrits
P.S. a 'valid' use for passing auto_ptr by value is to delibarately
relinquish
ownership. Perhaps a 'valid' use of this new lock would be:
void unlock(NewMutex::destructive_copy_lock) {};
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk