• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

CMutex/CSingleLock question

Hi,

I have a member variable that I want to lock so only one thread can update the same data at the same time.  I declare a CMutex object m_mutex and right before I use the member variable I want to lock I do:

BOOL CMyClass::Update()
{
CSingleLock lock(&m_mutex)
lock.Lock();

if (lock.IsLocked()) {
// Update the member.
lock.Unlock();
}
return TRUE;
}

My question is, what happens if the mutex is locked?  Would the application wait until the mutex is unlocked and then perform the new update or will it just return without making the update?  Basically, I want all updates to be carried out without updating the same member at the same time.  I do not want to miss any updates.

Thanks

0
StanChart
Asked:
StanChart
  • 5
  • 2
2 Solutions
 
AxterCommented:
Hi StanChart,
> >Would the application wait until the mutex is unlocked and then perform
> >the new update or will it just return without making the update?

With the above code, if it's lock by another thread, the application will wait until it gets unlocked.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
If it's locked by the same thread, then it will not wait.  It will just pass through.

If you're using CSingleLock, you don't need to explicitly lock and unlock it.
Instead, let the constructor and destructor do that for you automatically.
0
 
AlexFMCommented:
In your code program will wait on the line:
lock.Lock();

The same can be written by the following way:

BOOL CMyClass::Update()
{
    CSingleLock lock(&m_mutex, TRUE);   // program waits on this line and continues when mutex is successfully locked
    // Update the member.
    return TRUE;
}

Mutex is released in lock destructor.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
Example code:
BOOL CMyClass::Update()
{
CSingleLock lock(&m_mutex, TRUE); //Set second variable to TRUE so it will lock via constructor
//No need to lock, and no need to check if IsLocked
// Update the member.

//CSingleLock destructor will unlock, so no need for explicit unlock call
return TRUE;
}
0
 
AlexFMCommented:
Sorry, Axter, I didn't see your second post.
0
 
AxterCommented:
>>Sorry, Axter, I didn't see your second post.

No problem :-)
0
 
rcarlanCommented:
You may consider using a Critical Section instead of a Mutex - it depends on whether the resource you are trying to protect is shared across processes or just by threads belonging to the same process.

Critical Sections work only within a single process (i.e. for locking threads belonging to the same process). Named Mutexes work across processes. Critical Sections are faster and lighter than Mutexes.

MFC has a CCriticalSection class that inherits from the same base class as CMutex (CSyncObject) and can be used in exaclty the same way - i.e. you create an instance and then use CSingleLock to lock and unlock it.

Radu
0
 
AxterCommented:
>>right before I use the member variable I want to lock I do:

FYI:
I've recently created a thread safe wrapper class that can be used to make any instance of an object thread safe.
The wrapper class can be used to automatically synchronize access to the resource.

Check out the following link:
http://code.axter.com/sync_ptr.h

The above class can be used with either mutex logic or critical section logic.
It's also portable between Win32 platform and UNIX/Linux platform.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now