×
Namespaces

Variants
Actions
(Difference between revisions)

Recursive mutex

From Nokia Developer Wiki
Jump to: navigation, search
Narendrachinni (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix reviewer approved and ArticleMetaData)
 
Line 1: Line 1:
{{ReviewerApproved}}
+
{{ArticleMetaData <!-- v1.1 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20070626
 +
|author= [[User:PushL]]
 +
}}
 +
 
 
[[Category:Symbian C++]]
 
[[Category:Symbian C++]]
 
[[Category:Architecture/Design]]
 
[[Category:Architecture/Design]]
 
[[Category:Porting]]
 
[[Category:Porting]]
[[Category:Code Examples]]
+
[[Category:Code Snippet]]
 
'''Implementing recursive mutexes.'''
 
'''Implementing recursive mutexes.'''
  

Latest revision as of 08:13, 2 February 2012

Article Metadata
Article
Created: User:PushL (26 Jun 2007)
Last edited: hamishwillee (02 Feb 2012)

Implementing recursive mutexes.

(Note that Symbian 9 mutexes are recursive, so this applies to pre-v9)

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 CRecursiveMutex : public CBase
{
public:
CRecursiveMutex();
~CRecursiveMutex();
 
void Acquire();
void Release();
 
private:
RMutex iMutex;
TThreadId iOwner;
TInt iCount;
};
 
CRecursiveMutex::CRecursiveMutex() : iCount(0)
{
iMutex.CreateLocal();
}
 
CRecursiveMutex::~CRecursiveMutex()
{
iMutex.Close();
}
 
void CRecursiveMutex::Acquire()
{
TThreadId id = RThread().Id();
if (iOwner == id)
{
++iCount;
}
else
{
iMutex.Wait();
iCount = 1;
iOwner = id;
}
}
 
void CRecursiveMutex::Release()
{
if (--iCount == 0)
{
iOwner = 0;
iMutex.Signal();
}
}

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 2 February 2012, at 08:13.
84 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.

×