Re: Use Lock



David Alasiga wrote:
C++ for example allows you some really neat tricks that make the
compiler check that you first acquire a lock and then start
reading/writing.

I use C++. It sounds very interesting idea. Could you tell me how to do
it? Thanks for your reply.

Ian mentioned a facade - while I'm not sure if that is what it's called,
his description was what I had in mind. Basically, you provide a single
channel to the shared data (e.g. a function) that then returns a proxy
object. This proxy object does the locking and unlocking and further it
has e.g. an overloaded operator-> like a smart pointer that then gives you
access to the data.
There is another way that uses the fact that 'volatile' when applied to an
aggregate mostly works like 'const' does. As example, you can't call a
function via a reference to a volatile object unless the function is also
marked as volatile, just like you can't invoke a function unless it's
const when you have a reference to a const object. If you wrap the removal
of volatile and the locking into a struct or function, you won't be able
to use the object except for the few functions marked as volatile, too.
Combining this with the above, maybe using 'friend' to fine-tune access,
gives even greater security against abuse.

Uli

.



Relevant Pages

  • Re: strange grammar about volatile and operator overload
    ... actually in the definition/declaration of the global object, ... not marked as volatile. ... it's not treated as volatile or const. ... x is accessed via vr reference, ...
    (microsoft.public.vc.language)
  • Re: Lock-free proxy algorithm
    ... store(type* obj, deleter_t deleter); ... static void acquire(stored_type volatile* ptr); ... explicit scoped_handle(provider const& p); ...
    (comp.programming.threads)
  • Re: Share .cpp and .h along projects
    ... Because myarray is a pointer-to-const inside a const member function ... pointer, not an array. ... volatile pointer, not a pointer-to-volatile: ... protecting it against simple assignment, ...
    (microsoft.public.vc.language)
  • Re: InterlockedExchange and volatile
    ... though it's more commonly observed with const. ... Making it "volatile LPLONG" doesn't help, ... typedef LONG* LPLONG; ... so the following declare the same function and thus cause an error ...
    (microsoft.public.vc.mfc)
  • Re: Share .cpp and .h along projects
    ... Because myarray is a pointer-to-const inside a const member function ... pointer, not an array. ... can work on non-volatile variables, but volatile variables cannot be used by ... Since you admit that InterlockedXXX ...
    (microsoft.public.vc.language)