Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Implementing recursive mutexes

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: eswar_illuri (30 May 2007)
Last edited: hamishwillee (25 Jul 2012)

If you come from a POSIX environment, you'll find Symbian's support for synchronization primitives rather simplistic. This is in some way due to Symbian's preference for single threaded applications using the Active object idiom, thus making the use of multithreading often unnecessary. Sometimes, the need for recursive (reentrant) mutexes arises (note though that POSIX mutexes aren't recursive by default). Here is a possible implementation of a wrapper class over a RMutex:

class TRecursiveMutex
{
public:
TRecursiveMutex();
~TRecursiveMutex();
 
void Acquire();
void Release();
 
private:
RMutex iMutex;
TThreadId iOwner;
TInt iCount;
};
 
TRecursiveMutex::TRecursiveMutex() : iCount(0)
{
iMutex.CreateLocal();
}
 
TRecursiveMutex::~TRecursiveMutex()
{
iMutex.Close();
}
 
void TRecursiveMutex::Acquire()
{
TThreadId id = RThread().Id();
if (iOwner == id)
{
++iCount;
}
else
{
iMutex.Wait();
iCount = 1;
iOwner = id;
}
}
 
void TRecursiveMutex::Release()
{
if (--iCount == 0)
{
iOwner = 0;
iMutex.Signal();
}
}

Note:You'll notice this is a bare bones implementation. Potential things to be added (at least on debug builds) could be checking thread ownership and iCount being 0 upon destruction

This page was last modified on 25 July 2012, at 22:52.
45 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×