How to create a simple Thread

.From Forum Nokia WikiIn Symbian,

 

the class RThread is used to manipulate threads. An object of type RThread represents a handle to a thread, because the thread itself is a kernel object. An RThread object can be used to create or refer to another thread in order to manipulate it.

 

The base class of RThread is RHandleBase, which encapsulates the behavior of a generic handle and is used as a base class throughout Symbian OS to identify a handle to another object, often a kernel object.


RThread, RProcess, RMutex and RSession-Base are derived from RHandleBase.

 

 

Contents [hide]
1 Creating a Thread
2 Stopping a Running Thread
3 Killing a Running Thread
4 Getting the exit reason of a Thread
5 Getting Notification of Thread End.
 


Creating a Thread

/* Name of the new thread */

_LIT(KThreadName, "MyFirstThread");


/* Creates a thread belonging to the current process and opens this handle to that thread. */

TInt res = iThread.Create(KThreadName, ThreadEntryPoint, KDefaultStackSize, NULL, &iVariable);


/* Makes the thread eligible for execution.After a thread is created, it is put into a suspended state; the thread is not eligible to run until Resume() is called. */

iThread.Resume();


/* Control passes to this function when the thread is first resumed, i.e. when the thread is initially scheduled to run. This function just changes the value of the variable. */

TInt CThreadExampleAppUi::ThreadEntryPoint(TAny* param)

{  

TInt* para = static_cast<TInt *>(param);  

*para = 1;  

 return 1;

}

 

Stopping a Running Thread A running thread can be removed from the scheduler’s ready-to-run queue by a call to Suspend() on its thread handle. It still exists, however, and can be scheduled to run again by a call to Resume().

 


Killing a Running Thread A thread can be ended permanently by a call to Kill() or Terminate(), both of which take an integer parameter representing the exit reason. You should call Kill() or Terminate() to stop a thread normally, reserving Panic() for stopping the thread to highlight a programming error. If the main thread in a process is ended by any of these methods, the process terminates too.

 


Getting the exit reason of a Thread The manner by which the thread was stopped, and its exit reason, can be determined from the RThread handle of an expired thread by calling ExitType(), ExitReason() and ExitCategory(). The exit type indicates whether Kill(), Terminate() or Panic() was called or, indeed, if the thread is still running. The exit reason is the integer parameter value passed to the Kill() or Terminate() functions, or the panic reason. The category is a descriptor containing the panic category, Kill or Terminate. If the thread is still running, the exit reason is zero and the exit category is a blank string.

 


Getting Notification of Thread End. It is also possible to receive notification when a thread dies. A call to RThread::Logon() on a valid thread handle, passing in a TRequest- Status reference, submits a request for notification when that thread terminates.

The request completes when the thread terminates and receives the value with which the thread ended or KErrCancel if the notification request was cancelled by a call to RThread::LogonCancel().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值