×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:RMutex - Symbian mutex class

From Nokia Developer Wiki
Jump to: navigation, search
tepaa (Talk | contribs)
tepaa (Talk | contribs)
Line 79: Line 79:
 
     CMyThread* host = (CMyThread*)aParams;
 
     CMyThread* host = (CMyThread*)aParams;
 
      
 
      
    // Get shared data
 
    CShared* shared = host->Shared();
 
 
 
     TRAPD(err,
 
     TRAPD(err,
 
         // Add support for active objects
 
         // Add support for active objects

Revision as of 14:52, 19 May 2008


ID Creation date May 13, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95
Category Symbian C++ Subcategory Code Examples


Keywords (APIs, classes, methods, functions): RMutex, RSemaphore, RThread

Overview

Via RMutex you can be sure that e.g. threads do not change same value in the same time. RMutex.Wait() acquire the lock that opens with RMutex.Signal().

Following code is update into CS000867_-_RThread RThread example.

MMP file

The following capabilities and libraries are required:

CAPABILITY        NONE
LIBRARY euser.lib

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;
};


Source file

Create thread and give 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);

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 thread calls PeriodicTick() where it changes the shared data value and release 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();
}


Postconditions

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

See also

CS000867_-_RThread Using RThread

95 page views in the last 30 days.
×