Threads

Login to reply to this topic.
Mon, 2008-03-31 16:44
Joined: 2008-03-11
Forum posts: 26

Dear all, I would like to create 2 simple threads that do a very simple function but i don't know how. can anybody help me?


Mon, 2008-03-31 17:01
Joined: 2004-11-29
Forum posts: 1134
Re: Threads

First question to ask yourself: If they do a very simple function, why do you need to complicate it with threads?

you rarely really need threads in symbian.

If you need it, then this is I guess the shortest possible code to create and run one:

TInt MyThreadFunc(TAny* aArg)
{
//do something 
}

//... somewhere else:
RThread t;
t.Create(_L("MyThread"),MyThreadFunc,KDefaultStackSize,NULL,NULL);
t.Resume();

If you want to do it properly, you need to look at the functions Logon and Rendezvous too, and take into consideration how you want it to handle stack and heap etc....
Also know that you can't use any UI functions like menues dialogs and windows from the new thread without a lot of re-initialization of stuff (and many hours headake most probably)

Mon, 2008-03-31 17:22
Joined: 2005-12-07
Forum posts: 56
Re: Threads

search for "Active Objects" they the a nice alternate for threads in symbian.

Mon, 2008-03-31 17:46
Joined: 2007-09-23
Forum posts: 132
Re: Threads

Nabil has already been told here and on forum Nokia that he doesn't need to use threads if active objects can be used instead. He hasn't been able to get active objects working so he's trying threads instead. Geeeeze.

Mon, 2008-03-31 18:19
Joined: 2008-03-11
Forum posts: 26
Re: Threads

Thanks all of you. I've tried what alh said and I think there is a mistake in the line in which Create is called because I have errors. Are they correct? I've looked it up and there are 3 possibilities:
- TInt Create(const TDesC &aName, TThreadFunction aFunction, TInt aStackSize, TInt aHeapMinSize, TInt aHeapMaxSize, TAny *aPtr, TOwnerType aType=EOwnerProcess);
- TInt Create(const TDesC &aName, TThreadFunction aFunction, TInt aStackSize, RAllocator *aHeap, TAny *aPtr, TOwnerType aType=EOwnerProcess);
- TInt Create(const TDesC &aName, TThreadFunction aFunction, TInt aStackSize, TInt aHeapMinSize, TInt aHeapMaxSize, TAny *aPtr, TOwnerType aType=EOwnerProcess);
but none of them has 5 arguments! any suggestion?

Mon, 2008-03-31 18:52
Joined: 2003-12-05
Forum posts: 586
Re: Threads

Maybe you should also brush up your C++ skills a a bit, just a friendly suggestion. Each of those functions has a last parameter with default value. Meaning, you do not need to provide that value if you are satisfied with the default one.

Mon, 2008-03-31 19:22
Joined: 2007-09-23
Forum posts: 132
Re: Threads

I love it when people try to run before they can walk, you try to tell them to slow down and learn to walk first, but they ignore you, convinced they know best and determined to run, get into difficulties running and so try to hop instead or running, but just get into more difficultites hopping, so you tell them again to learnn to walk before trying to hop but they still ignore you. Determined that if they can't run they'll try hopping, and if they can't hop then they'll skip and if they can't skip then they'll dance around merrily oblivious like a whirrling dervish on steiroids until they collapse in a heap.

Mon, 2008-03-31 19:19
Joined: 2008-03-11
Forum posts: 26
Re: Threads

ok, sorry for the stupid question Sad.Thanks Andreas
I have an error in the create function, this line: iThreadOne.Create(_L("t1"), Exe1, KDefaultStackSize, KDefaultHeapSize,KDefaultHeapSize, NULL);

ERROR: function call '[RThread].Create(TPtrC16, int (CThread::*)(void *), {lval} const int, {lval} const unsigned int, {lval} const unsigned int, int)' does not match

Mon, 2008-03-31 19:43
Joined: 2007-09-23
Forum posts: 132
Re: Threads

Look at each parameter in turn and ask if it would compile if you assigned what you are passing as a parameter to what the compiler is telling you is expected i.e. if you had

TPtrC16 aname(_L("t1"));
...
TOwnerType atype(NULL);
etc.

Would it compile?

If these do, what about the others? Which one won't?

Tue, 2008-04-01 08:45
Joined: 2008-03-11
Forum posts: 26
Re: Threads

Thanks, I've forgotten to declare the Exe1 function as static!! Now this is solved. Smiling

Tue, 2008-04-01 09:32
Joined: 2008-03-11
Forum posts: 26
Re: Threads

Hi, I've got an kern-exec 3 when i run my application. Using breakpoints I've noticed that the panic appears just after the create function. This is my code:
void CThread::StartL()
{
iThreadOne.Create( _L("t1"), Exe1, 4096, KMinHeapSize, 256*KMinHeapSize, this); //After this line I have the error
iThreadTwo.Create( _L("t2") , Exe2, 4096, KMinHeapSize, 256*KMinHeapSize, this);
iThreadOne.Resume();
iThreadTwo.Resume();
}

TInt CThread::Exe1(TAny *aPtr)
{
CThread* obj = static_cast(aPtr);

int a=3, b=5, result;
result = b-a;
return KErrNone;
}

TInt CThread::Exe2(TAny *aPtr)
{
CThread* obj = static_cast(aPtr);
RFs fsSession;
_LIT(KFileName, "C:\\thread2.txt");
return KErrNone;
}

Any idea? Thanks

Tue, 2008-04-01 09:43
Joined: 2007-08-29
Forum posts: 128
Re: Threads

Hi,

The reasion for Kern-Exec 3 is this.

This panic is raised when an unhandled exception occurs. Exceptions have many causes, but the most common are access violations caused, for example, by dereferencing NULL. Among other possible causes are: general protection faults, executing an invalid instruction, alignment checks, etc.

you can search in SDK with this word "Kern-Exec 3" for more details.


Thanks & Regards
Ram

Symbian OS 9.1,S60 3rd edition MR,and CodeWarrior 3.1

Tue, 2008-04-01 10:00
Joined: 2004-11-29
Forum posts: 1134
Re: Threads

My guess is you have failed to create the CThread object before you call StartL() on it, so the "this" pointer is NULL.
Only way I could think of to get a Kern-Exec 3 on that line...

Tue, 2008-04-01 10:09
Joined: 2008-03-11
Forum posts: 26
Re: Threads

Thanks for your reply but I've got the same problem if I put 'NULL' instead of 'this'

Tue, 2008-04-01 13:09
Joined: 2004-11-29
Forum posts: 1134
Re: Threads

That doesn't solve the problem I was suggesting....

Check the value of this in your debugger. If the value is NULL you have a big problem.
That means you called StartL() on a NULL pointer. (without creating CThread first)
That would give a kern-exec3 on the line you mention.

The reason is the simple C++ fact that this line:

iThreadOne.Create( ... ); 

under the hood, really is:

this->iThreadOne.Create( ... ); 

Fri, 2008-04-04 10:09
Joined: 2007-09-03
Forum posts: 25
Re: Threads

Use the following code sample and modify your code for Create()

User::LeaveIfError(iThread.Create(KThreadName,ThreadFunc,KDefaultStackSize,KMinHeapSize,256*KMinHeapSize,iData,EOwnerThread));

  • Login to reply to this topic.