This page was last modified 08:19, 9 April 2008.
Recursive mutex
From Forum Nokia Wiki
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.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 为什么在线程中做CleanupStack::PushL(...)会出错? | pai_huai | Symbian | 3 | 2004-01-09 08:54 |
| About RChunk::CreateGlobal | yql | General Symbian C++ | 7 | 2007-12-28 12:15 |
| 对一个线程执行了Kill后 | wolf800 | Symbian | 1 | 2007-11-26 04:17 |
| "Erorr : Too many include paths!" | squirenokia | Symbian Tools & SDKs | 1 | 2005-05-24 21:59 |
| 7650 Http Connection Works! Source Code Example | sjwilks | Mobile Java General | 3 | 2003-02-10 14:50 |
