Re: Avoiding the anonymous variable declaration RAII error...
On 11 Mar, 18:42, "Eric J. Holtman" <e...@ericholtman.com> wrote:
I've burned myself twice in seven or so years doing this, and
it seems like there ought to be a clever way to avoid it:
I have a Mutex class, and a Locker class which acquires
a mutex in its constructor, and releases it in its destructor,
so you can write the standard code:
void
process_threaded_data ()
{
Locker lk (m_mutex);
.
.
.
}
However, it's a massive race condition just waiting to happen
if you forget the "lk", and just write:
void
process_threaded_data ()
{
Locker (m_mutex);
.
.
.
}
Is there some way to design class Locker so that writing the
second piece of code comes up as an error?
I've thought about it, and I can't figure out some clever way
involving hiding the constructor or destructor. I mean, you
can hide Locker's destructor, and provide a LockerWrapper,
but that just pushes the problem around, I don't think it
solves it.
Well I'm just kind of thinking out loud here, so forgive me if I'm
barking up the wrong tree, but I tried this using a boost scoped lock
(pretty much what you're trying to do it seems):
//---------------------------
#include <boost/thread/mutex.hpp>
boost::mutex my_mutex;
void f1()
{
boost::mutex::scoped_lock my_lock(my_mutex); // CORRECT!!
return;
}
void f2()
{
boost::mutex::scoped_lock (my_mutex); // WRONG!!
return;
}
int main()
{
f1();
f2();
return 0;
}
This gives me a compile-time error (using g++ v4.1.2) that says:
x.cc: In function 'void f2()':
x.cc:14: error: no matching function for call to
'boost::detail::thread::scoped_lock<boost::mutex>::scoped_lock()'
/usr/include/boost/thread/detail/lock.hpp:68: note: candidates are:
boost::detail::thread::scoped_lock<Mutex>::scoped_lock(Mutex&, bool)
[with Mutex = boost::mutex]
/usr/include/boost/thread/detail/lock.hpp:64: note:
boost::detail::thread::scoped_lock<boost::mutex>::scoped_lock(const
boost::detail::thread::scoped_lock<boost::mutex>&)
make: *** [x] Error 1
In the template, the scoped_lock is derived from boost::noncopyable,
and there is just the following constructor defined:
explicit scoped_lock(Mutex& mx, bool initially_locked=true)
: m_mutex(mx), m_locked(false)
{
if (initially_locked) lock();
}
Does this help?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]