×
Namespaces

Variants
Actions

如何使用RMutex

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 13 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000969


  • 开发伙伴平台:

S60 3rd Edition, MR


  • 详细描述

使用RMutex可以确保你的线程不会再同一时间试图改变同一个值。RMutex.Wait()提供一个锁机制,用RMutex.Signal()才能打开。


Contents

MMP文件

需要下列能力和链接库

CAPABILITY        NONE
LIBRARY euser.lib


头文件

在线程间共享iShareValue变量

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


源文件

生成一个线程,并将CShared数据指针传入:

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


线程调用RMutex::Wait()开始睡眠等待

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


在睡眠后调用PeriodicTick()改变共享数据对象的值,并用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数据

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


注意事项

线程依次去改变共享对象的值,如果调用RMutex::Wait()后另一个线程就要等待,在RMutex::Signal()之后才继续。

This page was last modified on 15 December 2011, at 09:42.
83 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×