Cross thread active objects

Login to reply to this topic.
Tue, 2007-10-09 08:51
Joined: 2007-09-10
Forum posts: 5

Hi,

My application has two threads. One is the master (same as the helloworldbasic example in the s60 sdk) and the other that I explicitly create, is the slave.
I have created active objects to communicate cross thread. So what I do is setactive an active object from master thread and RequestComplete on it from the slave (when I want my RunL to execute).
Problem: -
- Whenever I try RequestComplete from slave to master I get stray signal (not exactly at the same time.. but say 20-30 milliseconds later, enough to deduce that this RequestComplete was not the stray signal)
- I tried RequestComplete on the same activeobject from master thread itself (to test and got no stray signal)

1. Is there special ways of doing RThread::RequestComplete in case of cross thread communication?
2. Is the default Active Scheduler (installed by default in helloworldbasic application) capable of handling cross thread
communication?
- I am sure it is, just checking because the slave thread that I created also has an active scheduler and handles
RThread::RequestComplete very well. Only vice versa (slave->master) is not working!!!

Please help.


Tue, 2007-10-09 14:57
Joined: 2004-10-12
Forum posts: 11
Re: Cross thread active objects

Hi,

Problem is with calling RThread::RequestComplete(...) from another (slave) thread.
I imagine that you obtain Thread by calling RThread() function, and this function return current thread (Slave), what is right, not master thread as you assume. I have similar problem and solution is easy:

class CFoo : public CBase
{
    // constructor must be called from master thread, because we need to obtain right Thread ID
    CFoo()
    {
        iCurThread.Open( RThread().Id() );
    };
    ~CFoo()
    {
        iCurThread.Close();
    };

   // can be called from any thread
    void DoRequestComplet(...)
   {
      iCurThread.RequestComplet(...)
   };

private:
   RThread iCurThread;
};

I hope that, this you help. This work in my code very well.

Patrik

Tue, 2007-10-09 21:05
Joined: 2007-09-23
Forum posts: 159
Re: Cross thread active objects

Why do you need the threads, can't you just get rid of them and have a 'master' active object and a 'slave' active object?
Threads are only really needed when you need pre-emptive multitasking, do you need preemptive multitasking?

Wed, 2007-10-10 06:51
Joined: 2007-09-10
Forum posts: 5
Re: Cross thread active objects

Hi,

Vipako: This is true that RThread gives me the current thread handle only. I am storing a master thread handle (in a similar fashion) and use it whenever I need to signal the master thread.

Alert: The application has two threads because the slave is responsible for network send/recv (this is quite a huge application for streaming video over the internet) The slave thread receives AV data and decodes it not to mention rendering also is done by the slave thread. If I try doing all this in one thread my UI will become unresponsive (look like its hanged) whenever rendering or decoding would be in progress. (Rendering and decoding are expensive synchronous operations)

I forgot to mention one thing though, I get stray signal in my slave thread... The panic is given to it and it dies as soon as the panic happens. The UI (master) thread remains even after the panic. Does this mean that stray signal is being given to the slave thread? I installed a CActivescheduler in the slave thread and could schedule most of the AOs there. It seems really complicated as how can signalling the master thread cause stray signals in the slave thread.

Thoughts...

Wed, 2007-10-10 20:18
Joined: 2007-09-23
Forum posts: 159
Re: Cross thread active objects

"Alert: The application has two threads because the slave is responsible for network send/recv (this is quite a huge application for streaming video over the internet) The slave thread receives AV data and decodes it not to mention rendering also is done by the slave thread. If I try doing all this in one thread my UI will become unresponsive (look like its hanged) whenever rendering or decoding would be in progress. (Rendering and decoding are expensive synchronous operations)."

That's exactly what active objects are used for - to stop a UI from becomming unresponsive. You could have one active object to receive the data and another to render it. If either takes a long time that is still what active objects are for - you break a long running task such as rendering down into smaller steps with each smaller steps executed within a separate invocation of RunL().
However if you have implemented the rendering functionality yourself and it is synchronous then you can't do this unless you re-implement it, in which case threads may be easier, however if the rendering functions are Symbian functions then I cannot believe that they would have been implemented in a purely synchronous way.

Thu, 2007-10-11 06:53
Joined: 2007-09-10
Forum posts: 5
Re: Cross thread active objects

Hi,

1. I am using the CMMFCodec::ProcessL to decode data. It is standard API for decoding encoded data on Symbian. AFAIK standard decoders are all synchronous.
2. Even if I implement an asynchronous decoder, I don't think a single thread would be able to handle network (300 Kbps AV), Decoding, Rendering altogether. Please keep in mind that audio-video streaming operations are very time critical and I can't risk running some UI event RunL while I am painting the screen(or receiving data, or decoding data for rendering).

Thu, 2007-10-11 07:04
Joined: 2006-10-07
Forum posts: 131
Re: Cross thread active objects

Using second thread is the way to go if you want to achieve latency-free playback. Otherwise other active objects will interfere with your playback active object.

Vipako: This is true that RThread gives me the current thread handle only. I am storing a master thread handle (in a similar fashion) and use it whenever I need to signal the master thread.

To answer the original question: I have had similar problems with inter-thread communication quite a while ago and the cause is the fact that RThread() really stores the handle of the current thread, that is, if you save the handle in the master thread and access it in the slave thread, you will get a handle of the slave thread which is indeed "current"! To get the desired behaviour, I had to get thread handle by iHandleToSave.Open(RThread().Id()).

Thu, 2007-10-11 07:11
Joined: 2007-09-10
Forum posts: 5
Re: Cross thread active objects

So rather than saving the handle I should save the ID and while using should create a RHandle to it.

Thanks for the reply... will try and let you guys know. However, one thing bothers me though, If RHandle (when accessed form slave thread) would actually be a handle to the slave thread (even if I created it in main thread) how could the RunL actually get trigerred in the main thread when I say RThread::RequestComplete from slave thread. (Pl. refer my original posting)

Wed, 2008-09-24 16:13
Joined: 2008-09-24
Forum posts: 1
Re: Cross thread active objects

Hi casual_kumar,
Did you managed to solve this problem
I got the same error and don`t know how to solve it.

Thu, 2008-09-25 14:33
Joined: 2007-09-20
Forum posts: 114
Re: Cross thread active objects

Hi,

Wouldn't it be better to use RMsgQueue to notify the events to other threads? The master thread posts the messages to the que and the slave thread keeps listening to the que.


Chao,
Raghav

  • Login to reply to this topic.