an idea to lock data by removing the reference:

class A
{
   Lockable!Data data;
}

The idea is that when the data is going to be used, the user locks the data. The trick here is that data is a pointer to the data and the pointer is set to null when locked so no other data can use it(they see a null reference). To unlock, the data is reassigned:

auto d = data.lock(); // A.data is now null deals with sync issues
//use d
d = data.unlock(d); // restores A.data (A.data = d;) and sets d to null so any future reference is an error(could produce bugs but should mostly be access violations)


Anyone else trying to use data will see that it is null while it is locked.

This basically pushes the standard locking mechanisms in to the Lockable!data(first come first serve) and code that has not captured the data see's it simply as null.

Anyone use know if there exists an idiom like this and what it is called? Maybe some idiomatic code that is efficient?


Ideally I'd want to be able to treat the Lockable!Data as Data. Right now I have to classes(or pointers to structs) and few weird hacks. I think what I'll have to end up doing is having a Locked!Data structure that is returned instead or use an UFCS. The idea being to attach the lock and unlock methods.









Reply via email to