Variable lock between rmutex Symbian threads

Source: Internet
Author: User

Development partner platform:
S60 3rd edition, Mr

Detailed description
Using rmutex ensures that your thread does not try to change the same value at the same time. Rmutex. Wait () provides a lock mechanism that can be opened using rmutex. Signal.

MPs File
The following capabilities and link libraries are required:

Code:
CAPABILITY        NONE
LIBRARY euser.lib

Header file
Share isharevalue variables between threads

Code:
class CShared : CBase
{
public:
static CShared* NewL();
virtual ~CShared();

private:
CShared();

public:
RMutex iMutex;
// Shared data
TInt iSharedValue;
};

Source File
Generate a thread and pass the cshared Data Pointer:

Code:
// Create shared data
iSharedData = CShared::NewL();

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

iMyThread2 = CMyThread::NewL(*this);
iMyThread2->SetShared(iSharedData);

The thread calls rmutex: Wait () to start sleep.

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

Call periodictick () after sleep to change the value of the shared data object and release it with rmutex: Signal ().

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

Complete cshared data

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

Notes
The thread changes the value of the shared object in sequence. If rmutex: Wait () is called, the other thread will wait until rmutex: Signal () is called.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.