×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:RMutex - Symbian mutex class

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (moved CS000969 - RMutex to RMutex - Symbian mutex class: Merge into wiki.)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
{{Archived|timestamp=20120313125720|user=roy.debjit| }}
 
 
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:S60 3rd Edition (initial release)]][[Category:Code Snippet]]
 
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:S60 3rd Edition (initial release)]][[Category:Code Snippet]]
 +
{{Archived|timestamp=20120313125720|user=roy.debjit| }}
 +
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
Line 28: Line 29:
 
    
 
    
 
==Overview==
 
==Overview==
Using  {{Icode|RMutex}} you can make sure that, for example, threads do not change the same value at the same time.
+
{{Abstract|Using  {{Icode|RMutex}} you can make sure that, for example, threads do not change the same value at the same time. {{Icode|RMutex.Wait()}} acquires a lock that opens with {{Icode|RMutex.Signal()}}.}}
{{Icode|RMutex.Wait()}} acquires a lock that opens with {{Icode|RMutex.Signal()}}.
+
  
The following code is an update to the [[RThread - Symbian threading example]] example.
+
The following code is an update to the [[Archived:RThread - Symbian threading example]] example.
  
 
==MMP file==
 
==MMP file==
Line 167: Line 167:
  
 
==See also==
 
==See also==
[[RThread - Symbian threading example]] Using RThread
+
[[Archived:RThread - Symbian threading example]] Using RThread

Latest revision as of 00:10, 2 September 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N95
Compatibility
Platform(s): S60 3rd Edition, MR
S60 3rd Edition (initial release)
Article
Keywords: RMutex, RSemaphore, RThread
Created: tepaa (13 May 2008)
Last edited: lpvalente (02 Sep 2012)

Contents

[edit] Overview

Using RMutex you can make sure that, for example, threads do not change the same value at the same time. RMutex.Wait() acquires a lock that opens with RMutex.Signal().

The following code is an update to the Archived:RThread - Symbian threading example example.

[edit] MMP file

The following capabilities and libraries are required:

CAPABILITY NONE
LIBRARY euser.lib

[edit] Header file

Shared data iSharedValue between threads.

class CShared : CBase
{
public:
static CShared* NewL();
virtual ~CShared();
 
private:
CShared();
 
public:
RMutex iMutex;
// Shared data
TInt iSharedValue;
};


[edit] Source file

Create a thread and give the CShared data pointer in it.

// Create shared data
iSharedData = CShared::NewL();
 
// Create threads
iMyThread = CMyThread::NewL(*this);
iMyThread->SetShared(iSharedData);
 
iMyThread2 = CMyThread::NewL(*this);
iMyThread2->SetShared(iSharedData);

The thread fuction saysRMutex::Wait() and then sleeps.

TInt CMyThread::ThreadFunction(TAny* aParams)
{
// Add cleanup stack support.
CTrapCleanup* cleanupStack = CTrapCleanup::New();
 
// Get pointer to thread host
CMyThread* host = (CMyThread*)aParams;
 
TRAPD(err,
// Add support for active objects
CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
CleanupStack::PushL(activeScheduler);
CActiveScheduler::Install(activeScheduler);
 
// Create and start CPeriodic class that is executed in this thread
CPeriodic* periodic = CPeriodic::NewL(CActive::EPriorityLow);
CleanupStack::PushL(periodic);
periodic->Start(host->Interval(),host->Interval(),
TCallBack(host->PeriodicTick, host));
 
// NOTE: When adding CActiveScheduler support for threads we have to
// add at least one active object in it or it fails on
// CActiveScheduler::Start().
// CPeriodic is derived from CActive active object so that is good for
// this example.
 
// --> Thread execution starts
CActiveScheduler::Start();
// --> Thread execution ends (waiting for CActiveScheduler::Stop())
 
CleanupStack::PopAndDestroy(periodic);
CleanupStack::PopAndDestroy(activeScheduler);
);
 
 
host->ThreadExecuted(err);
delete cleanupStack;
return KErrNone;
}

After sleeping the thread calls PeriodicTick() where it changes the shared data value and releases RMutex::Signal().

TInt CMyThread::PeriodicTick(TAny* aObject)
{
CMyThread* mythread = (CMyThread*)aObject;
if (mythread)
{
// ---> Acquire the lock
mythread->Shared()->iMutex.Wait();
 
// Change shared data value
mythread->Shared()->iSharedValue++;
 
// ---> Release the lock
mythread->Shared()->iMutex.Signal();
 
// Thread is executed once so time to stop it
CActiveScheduler::Stop();
 
// After this execution continues from CActiveScheduler::Start()
}
// Does not continue again
// Note: Does not work with this CPeriodic class
return EFalse;
}

CShared data implementation

CShared* CShared::NewL()
{
CShared* self = new (ELeave) CShared();
return self;
}
CShared::CShared()
{
iMutex.CreateLocal();
}
CShared::~CShared()
{
iMutex.Close();
}


[edit] Postconditions

Threads change shared data value in turns. The other thread waits if RMutex::Wait() is called and continues after RMutes::Signal().

[edit] See also

Archived:RThread - Symbian threading example Using RThread

This page was last modified on 2 September 2012, at 00:10.
68 page views in the last 30 days.
×