Tuesday, September 16, 2008

Thread on Symbian

-------------------------------Threads on Symbian OS--------------------------------
---- Threads are basic entity of a process. How do we create a thread in Symbian? Before going to discuss about threads on Symbian we should first understand thread structure. What are the minimum requirements for creating a thread?

---- Thread is called single execution unit of a process. It’s necessary to have a stack for each thread because without stack we cannot create thread in application.

Why I cannot create a thread without stack?
---- Thread is nothing but, collection of function or single function. Function is for processing data. Thread has to store those data somewhere in the memory, it can store in data block so that the life time of that variable will be until the program termination. But for local manipulation thread should use stack for storing or retrieving data temporally. Every function call is using stack, how? Compiler stores the return address in the stack before transferring control to that function. So it’s mandatory, that each thread should have stack. Otherwise programming is not possible.


Process structure:



Threads in Symbian
----Class RThread is used to create thread in Symbian. RThread class has a function is called create() is used to create a thread. What and all I have to pass to create a thread? Yes we will see one by one this

RThread iThread;
iThread.Create(Threadname, function pointer, stack size, Heap pointer, thread arguments,thread ownership)

Agruments:
TBufC iThreadName(_L(“ExampleThread”));
TInt ThreadInit(TAny *obj);
TInt iStackSize = 32000;
NULL
NULL
Default it will take EOwnerProcess

Thread Creation:
Class CThreadExample
{
RThread iThread;
Public:
CThreadExample();
Friend TInt ThreadInit(TAny *obj);
};
CThreadExample::CThreadExample()
{
TInt err = iThread.Create(iThreadName, ThreadInit, iStackSize, NULL, NULL);
If (err != KErrNone)
{
RDebug::Printf(“Thread creation Failed\n”);
User::Leave(err);
}
iThread.Resume();
}

The function which we register in create call will be called once the thread has been created successfylly.

TInt ThreadInit(TAny *obj)
{
RDebug::Printf(“Thread successfully created Its now ready to run\n”);
iThread.Kill(KErrNone);
}
....:-)

No comments: