Good lock_guard<mytype> example

Hello, The pattern goes something like this: boost::lock_guard<mytype> guard(*this). However, compiler is asking for a lock or unlock. I don't know what this is, or what it should be. Thank you... Regards, Michael Powell

Hello Michael, *this is a pointer to the mutex that the 'lock_guard' is using. The name "this" is unfortunate since it overlaps with an object's 'this' pointer... unless your class 'mytype' is derived from a mutex.... I've usually seen it used on just a boost::mutex. boost::mutex the_lock; and later in some code associated with the lock, you could activate it in a block by doing; boost::lock_guardboost::mutex lock(the_lock); ...do stuff.... Consider looking at: http://www.boost.org/doc/libs/1_53_0/doc/html/thread/synchronization.html#th... and searching for lock_guard I think it has a pretty decent example there. On 4/1/2013 4:12 PM, Michael Powell wrote:
Hello,
The pattern goes something like this: boost::lock_guard<mytype> guard(*this).
However, compiler is asking for a lock or unlock. I don't know what this is, or what it should be.
Thank you...
Regards,
Michael Powell
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, Apr 1, 2013 at 6:20 PM, John Rocha
Hello Michael,
*this is a pointer to the mutex that the 'lock_guard' is using. The name "this" is unfortunate since it overlaps with an object's 'this' pointer... unless your class 'mytype' is derived from a mutex....
I believe just what I'm looking for. From a previous reply, I don't "really" want to derive from lockable classes, I just want to guard this class as a potentially thread-safe operation.
I've usually seen it used on just a boost::mutex.
boost::mutex the_lock;
and later in some code associated with the lock, you could activate it in a block by doing;
boost::lock_guardboost::mutex lock(the_lock); ...do stuff....
Consider looking at: http://www.boost.org/doc/libs/1_53_0/doc/html/thread/synchronization.html#th... searching for lock_guard I think it has a pretty decent example there.
On 4/1/2013 4:12 PM, Michael Powell wrote:
Hello,
The pattern goes something like this: boost::lock_guard<mytype> guard(*this).
However, compiler is asking for a lock or unlock. I don't know what this is, or what it should be.
Thank you...
Regards,
Michael Powell
_______________________________________________ Boost-users mailing listBoost-users@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Le 02/04/13 03:20, Michael Powell a écrit :
On Mon, Apr 1, 2013 at 6:20 PM, John Rocha
mailto:jrr@cisco.com> wrote: Hello Michael,
*this is a pointer to the mutex that the 'lock_guard' is using. The name "this" is unfortunate since it overlaps with an object's 'this' pointer... unless your class 'mytype' is derived from a mutex....
I believe just what I'm looking for. From a previous reply, I don't "really" want to derive from lockable classes, I just want to guard this class as a potentially thread-safe operation.
I have added lastly some classes to Boost.Thread that could help you. *synchronized_value*: uses internally a mutex to protect all the operations (1.54). It is based on the paper of Anthony Williams "Enforcing Correct Mutex Usage with Synchronized Values". *externally_locked*: enforce the use of an external lock to access the object (1.53). The mutex used to protect the access canbe shared between several classes/instances. It is based on the paper of Andrei Alexandrescu "Multithreading and the C++ Type System". HTH, Vicente

AMDG On 04/01/2013 04:12 PM, Michael Powell wrote:
Hello,
The pattern goes something like this: boost::lock_guard<mytype> guard(*this).
However, compiler is asking for a lock or unlock. I don't know what this is, or what it should be.
You can't lock an arbitrary object. You can only lock a mutex. If you really want to make an object act like a mutex, see http://www.boost.org/doc/libs/1_53_0/doc/html/thread/synchronization.html#th... In Christ, Steven Watanabe
participants (4)
-
John Rocha
-
Michael Powell
-
Steven Watanabe
-
Vicente J. Botet Escriba