Sorry, there's at least one '}' missing at ~CMyMutexLock ...
Main Topics
Browse All TopicsHi,
I am having a C++ function and i want to use mutex lock for the entire function only if certain condition is true. If that condition is false then i dont want to use mutex lock. Please suggest me simple wayu to do it.
Here problem i m facing is, that i am using my own mutext class which constructor and destructor is hadnlong locking and unlocking. So using this class will be more convenient for me other my code is having multiple return path and exception handlers. So using this class will avoid code redundancy and will increase correctness.
When i create object of this class then automatically it will lock the whole method. and whenever object will destroy it will unlock the mutex.
For example say my mutex class is: CMyMutexLock
But if i write such if condition in code then its scope will be limited to the if block only.
Is there any easy way so that by using #define i can achieve it?
#define LOCK CMyMutexLock obj;
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You could use an auto_ptr and create the mutex object on the heap.
http://www.cplusplus.com/r
void f(bool bNeedToLock)
{
auto_ptr< CMyMutexLock > pMutex; // Scoped to function f but only created if needed
if(bNeedToLock)
pMutex.reset(new CMyMutexLoc);
//remaining function code
}
Business Accounts
Answer for Membership
by: ZoppoPosted on 2009-10-09 at 07:12:55ID: 25535145
Hi harish_dixit1,
the code you posted won't wokr as you need since the 'obj' will be destroyed immediately after the 'if' ends, not when the function terminates.
My suggestion is that you extend the 'CMyMutexLock' class in a way you can instantiate it optionally in a none locked state and than later lock it 'manually', i.e.:
class CMyMutexLock
{
bool m_bLocked;
...
public:
CMyMutexLock( bool bIntiallyLocked = true ) : m_bLocked( false )
{
if ( false != bInitiallyLocked )
{
Lock();
}
}
~CMyMutexLock()
{
if ( false != m_bLocked )
{
Unlock();
}
void Lock()
{
m_bLocked = true;
... // do the locking here
}
void Unlock()
{
m_bLocked =false;
// do the unlocking here
}
}
};
Then you can do something like this:
void f(bool bNeedToLock)
{
CMyMutexLock obj( false );
if(bNeedToLock)
//need to lock the mutex object
obj.Lock();
//remaining function code
}
Hope that helps,
ZOPPO