Archived:RMutex - Symbian mutex class
m (Lpvalente -) |
|||
| (13 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:Symbian C++]][[Category:Code Snippet]][[Category:S60 3rd Edition (initial release)]][[Category:Code Snippet]] | |
| − | + | {{Archived|timestamp=20120313125720|user=roy.debjit| }} | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | {| | + | |
| − | + | ||
| − | + | ||
| − | |} | + | |
| + | {{ArticleMetaData <!-- v1.2 --> | ||
| + | |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= Nokia N95 | ||
| + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |platform= S60 3rd Edition, MR | ||
| + | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= RMutex, RSemaphore, RThread | ||
| + | |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= 20080513 | ||
| + | |author= [[User:Tepaa]] | ||
| + | <!-- The following are not in current metadata --> | ||
| + | |subcategory= Code Examples | ||
| + | |id= CS000969 | ||
| + | }} | ||
| + | |||
==Overview== | ==Overview== | ||
| − | + | {{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()}}.}} | |
| − | value | + | |
| − | + | ||
| − | + | The following code is an update to the [[Archived:RThread - Symbian threading example]] example. | |
==MMP file== | ==MMP file== | ||
The following capabilities and libraries are required: | The following capabilities and libraries are required: | ||
<code cpp> | <code cpp> | ||
| − | CAPABILITY | + | CAPABILITY NONE |
| − | LIBRARY | + | LIBRARY euser.lib |
</code> | </code> | ||
==Header file== | ==Header file== | ||
| − | Shared data | + | Shared data {{Icode|iSharedValue}} between threads. |
<code cpp> | <code cpp> | ||
class CShared : CBase | class CShared : CBase | ||
| Line 48: | Line 53: | ||
public: | public: | ||
| − | RMutex | + | RMutex iMutex; |
// Shared data | // Shared data | ||
| − | TInt | + | TInt iSharedValue; |
}; | }; | ||
</code> | </code> | ||
| Line 56: | Line 61: | ||
==Source file== | ==Source file== | ||
| − | Create thread and give | + | Create a thread and give the {{Icode|CShared}} data pointer in it. |
<code cpp> | <code cpp> | ||
// Create shared data | // Create shared data | ||
| Line 69: | Line 74: | ||
</code> | </code> | ||
| − | + | The thread fuction says{{Icode|RMutex::Wait()}} and then sleeps. | |
<code cpp> | <code cpp> | ||
TInt CMyThread::ThreadFunction(TAny* aParams) | TInt CMyThread::ThreadFunction(TAny* aParams) | ||
| Line 79: | Line 84: | ||
CMyThread* host = (CMyThread*)aParams; | CMyThread* host = (CMyThread*)aParams; | ||
| − | |||
| − | |||
| − | |||
TRAPD(err, | TRAPD(err, | ||
// Add support for active objects | // Add support for active objects | ||
| Line 115: | Line 117: | ||
</code> | </code> | ||
| − | After sleeping thread calls | + | After sleeping the thread calls {{Icode|PeriodicTick()}} where it changes the shared |
| − | data value and | + | data value and releases {{Icode|RMutex::Signal()}}. |
<code cpp> | <code cpp> | ||
TInt CMyThread::PeriodicTick(TAny* aObject) | TInt CMyThread::PeriodicTick(TAny* aObject) | ||
| Line 143: | Line 145: | ||
</code> | </code> | ||
| − | + | {{Icode|CShared}} data implementation | |
<code cpp> | <code cpp> | ||
CShared* CShared::NewL() | CShared* CShared::NewL() | ||
| Line 162: | Line 164: | ||
==Postconditions== | ==Postconditions== | ||
| − | Threads | + | Threads change shared data value in turns. The other thread waits if RMutex::Wait() is called and continues after RMutes::Signal(). |
==See also== | ==See also== | ||
| − | [[ | + | [[Archived:RThread - Symbian threading example]] Using RThread |
| − | + | ||
| − | + | ||
Latest revision as of 00:10, 2 September 2012
Contents |
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.
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 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();
}
Postconditions
Threads change shared data value in turns. The other thread waits if RMutex::Wait() is called and continues after RMutes::Signal().
See also
Archived:RThread - Symbian threading example Using RThread

