Boost logo

Boost :

From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2008-04-03 09:37:12


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 02 April 2008 16:46 pm, klaus triendl wrote:
> Phil Endecott schrieb:
> > i.e. LockableRef contains a reference to the mutex and one element of
> > its "parent" Lockable<>, and presents the same interface as a data-full
> > Lockable<>.
> >
> > But because Lockable and LockableRef aren't actually the same type, I
> > can't write
> >
> > void start_updater(Lockable<int> i);
> >
> > and use it with both.
> >
> > Is this something that you have thought about?
>
> Well, I didn't think about this one. But what do you think of the
> following?
>
>
> <code>
> template<typename T_type, typename T_mutex>
> struct lockable //: ...
> {
> // ...
>
> // return a lockable_ref, see below
> lockable_ref<std::tr1::remove_extent<T_type>, T_mutex>
> operator [](std::size_t idx)
> {
> return lockable_ref<std::tr1::remove_extent<T_type>,
> T_mutex>(access_volatile()[idx], mutex());
> }
> };

This would only support array-like types. Are you familiar with the aliasing
constructor of shared_ptr? I put something similar in poet::monitor_ptr, but
it's only in cvs at the moment. Anyways, it's a more general solution to
this kind of situation, although it doesn't provide much of a safeguard
against misuse. Anyways, your example snippet would look something like
this:

<code>
void start_updater(poet::monitor_ptr<int> i)
{
//...
}

poet::monitor_ptr<array<int, 100> > values;
poet::monitor_ptr<int> one_value;

//...

for (int i=0; i<100; ++i) {
    start_updater(poet::monitor_ptr<int>(values, &values->at(i)));
}

start_updater(one_value);
</code>

- --
Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFH9N2N5vihyNWuA4URAieWAKDPq4o2VKHIoRKeiBZmz3gN6N7VIgCg5MgQ
PnAdePswe+M7crNq7SLcOlk=
=IC+K
-----END PGP SIGNATURE-----


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk