Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 10:18, 20 May 2008.

CS000969 - RMutex

From Forum Nokia Wiki


ID CS000969 Creation date May 20, 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

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 CS000867 - 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 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

CS000867 - RThread Using RThread

Related Discussions
Thread Thread Starter Forum Replies Last Post
How to achieve exlusive library access when using CActiveSchedulerWait? di97mswg General Symbian C++ 6 2007-10-25 14:27
how to Synchonize member Variable in thread? Tanya Symbian Networking & Messaging 0 2007-06-26 10:53
Help! some problems when including e32std.h chyna_bo General Symbian C++ 1 2004-11-17 00:51
RSemaphore and starting servers... profoundwhispers General Symbian C++ 4 2005-08-11 16:36
Porting issue related to socket muralidharvvk Porting Symbian C++ to S60 4 2007-03-08 14:18
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX