*線程
- 線程是多任務的基礎,允許多個代碼序列同時執行
- 在Symbian作業系統中,可以同時建立多個線程,但是會帶來管理複雜和巨大的系統開銷
- 在Symbian作業系統中,操作線程的類是RThread,其對象表示一個線程的控制代碼
- RThread的基類是RHandleBase,它封閉了通用控制代碼的行為,用於表示Symbian作業系統中對象的控制代碼
*建立和啟動線程
TInt Create(const TDesC& aName, THreadFunction aFunction, TInt aStackSize, RHeap* aHeap, TAny* aPtr, TOwnerType aType = EOWnerProcess)
TInt threadFunc(TAny*)
{
for (TInt i=0; i<10; i++)
{
User::InfoPrint(_L("Thread"));
User::After(1000000);
}
return 0;
}
void StartThreadL()
{
RThread thd;
User::LeaveIfError(thd.Create(_L("MyThread"),threadFunc,KDefalutStactSize,NULL,NULL);
thd.Resume();//線程建立後是掛起狀態,這一步是啟動線程
}
*開啟已經存在的線程
RThread thd;
TInt rc = thd.Open(_L("MyThread"));
if (rc != KErrNone)
{
}
if (thd.EXitType() == EExitPending)//如果進程已經存在並且還在運行
thd.Suspend();//掛起進程,既等待進程運行完畢
……
thd.Resume(); //繼續線程執行
*線程的優先順序(均是相對所在進程的優先順序而言)
- EPriorityNull(-30)
- EPriorityMuchLess(-20)
- EPriorityLess(-10)
- EPriorityNormal(0)
- EPriorityMore(+10)
- EPriorityMuchMore(+20)
- EPriorityRealTime(+30)
*終止線程
- 可以使用RThread::Kill(TInt aReason)來終止線程
- RThread提供ExitType()和ExitReason()來返回線程終止的原因
- ExitType()傳回值包括
·EExitKill: 線程函數返回或顯示調用Kill()函數
·EExutPanic:線程因為嚴重錯誤而結束
·EExitPending:線程仍在運行
- ExitReason()傳回值包括
·傳回碼:線程函數正常返回
·終結代碼:線程調用Kill()函數
·錯誤碼:線程因為嚴重錯誤而退出
·零:線程仍在運行