Boost logo

Boost Users :

From: Kevin Heifner (heifner_k_at_[hidden])
Date: 2006-02-21 21:53:21


Dhanvi Kapila wrote:
> Kevin Heifner wrote:
>> Dhanvi Kapila wrote:
>>
>>> So how you delete a lock ?? Is there any other safe way to delete a lock ?
>>>
>> I don't know what you are asking. Can you provide an example?
>> Use the scoped lock concept. If you need to hide boost from the
>> rest of the application then provide a wrapper around scoped lock
> I need to hide boost locks from the rest of my code. I have a factory
> class that creates locks ( we have ACE and Boost locks implemented).
> to create the locks, in boost, we use the new keyword and to unlock (
> release the lock ) we delete the locks. The mutex object using which the
> locks are created are shared among the threads .
>
> // create lock
> //my_mutex is a mutex factory object with b_mutex as a public member of
> type Boost::mutex
> boost::mutex::scoped_lock* lk = new boost::mutex::scoped_lock(
> my_mutex->b_mutex );
>
> // deletion of lock
> delete lk;
>
> is this a correct way to do this ??

If you don't mind including ACE or Boost header files whichever
you are using at the moment then something like this would work:

// file MyMutex_ACE.h
#include "ace/Synch_T.h"

struct MyMutex {
     ACE_SYNCH_MUTEX mutex_;
};

#define MY_SCOPED_LOCK(M) ACE_GUARD(ACE_SYNCH_MUTEX, ace_mon,
M.mutex_)

// file MyMutex_Boost.h
#include <boost/thread.hpp>

struct MyMutex {
     boost::mutex mutex_;
};

#define MY_SCOPED_LOCK(M) boost::mutex::scoped_lock(M.mutex_)

// file MyMutex.h

#ifdef ACE
#include "MyMutex_ACE.h"
#else
#include "MyMutex_Boost.h"
#endif

// application code
#include "MyMutex.h"

MyMutex mutex;

MY_SCOPED_LOCK(mutex);

If you want to not include ACE/Boost header files in your
application code then something like the following would work:

// MyMutex.h

#incluce <boost/scoped_ptr.hpp>

struct MyMutex {
     MyMutex();
     struct MyMutexImpl;
     boost::scoped_ptr<MyMutexImpl> impl_;
};

class MyScopedLock {
   public:
     MyScopedLock(MyMutex& m);
   private:
     struct MyScopedLockImpl;
     boost::scoped_ptr<MyScopedLockImpl> impl_;
};

// MyMutex.cpp

#include "MyMutex.h"

#ifdef ACE

#include "ace/Synch_T.h"

struct MyMutex::MyMutexImpl {
   ACE_SYNCH_MUTEX mutex_;
};

struct MyScopedLock::MyScopedLockImpl {
   MyScopedLockImpl(MyMutex& m) : lock_(m.impl_->mutex_) {}
   ACE_Guard<ACE_SYNCH_MUTEX> lock_;
};

#else

#include <boost/thread.hpp>

struct MyMutex::MyMutexImpl {
   boost::mutex mutex_;
};

struct MyScopedLock::MyScopedLockImpl {
   MyScopedLockImpl(MyMutex& m) : lock_(m.impl_->mutex_) {}
   boost::mutex::scoped_lock lock_;
};

#endif

MyMutex::MyMutex() : impl_(new MyMutexImpl) {}

HTH,
KevinH

-- 
Kevin Heifner  heifner @ ociweb.com  http://heifner.blogspot.com
           Object Computing, Inc. (OCI) www.ociweb.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net