c++ - Looking for a function (or a macro) to return a boost::scoped_lock -
i'm looking code shortening idea. i'm using boost::scoped_lock
lock boost::mutex
want shorten amount of code i'm writing.
currently have mutex
defined in class , member field called _sync
. when want lock, have write:
scoped_lock<mutex> lock(_sync);
the tricky part scoped lock, assume if write static function return scoped_lock, unlock gets out of function scope of static function:
static scoped_lock<mutex> lock(mutex& sync) { return scoped_lock<mutex>(sync); }
this approach make easy type:
public void object::modify() { lock(_sync); // <-- nice , short! ;) // modify object //.. // mutex unlocked when leave scope of modify }
is assumption correct? scoped_lock
unlock when it's returned static function?
#define lock(a) scoped_lock<mutex> scopedlockvar(a) public void object::modify() { lock(_sync); // <-- nice , short! ;) // modify object //.. // mutex unlocked when leave scope of modify }
you should use safe name define... compiler uses find , replace defines...
Comments
Post a Comment