views:

223

answers:

2

Hi,

1) In symbian c++ thread is not recommended. Instead of that they recommend active object for multi tasking. Presently I am using QT to develop a application in symbian. Since there is no active object in QT I thought of using thread. My question is , can I use thread, is it is recommended. If it is not recommended, how to achieve multitasking.

2) I have created a sample thread class as shown bellow. When I call test function from the constructer of the main window thread will start but UI will be in hung state, infact main window itself will not be displayed. Please help me to solve the problem.

class CSampleThread: public QThread

{
    Q_OBJECT

public:
    CSampleThread(QObject *parent = 0) : QThread(parent)
    {}

    virtual ~CSampleThread()
    {}

    void test(){
        QThread::start(LowPriority);
    }

protected:

    void run()
{
    while(true){}

    }
};
A: 

Could be that the "while(true)" is starving the main thread for CPU cycles. Try adding a call to yield() or sleep() in the loop body.

Morten
Yes , I added sleep(1000); in the while loop. It could display the UI.Let us consider if we have a loop which don’t have sleep and yield then it will hang. Then don’t the os take care of context switching.
Umesha MS
A: 

You are right in saying that, for the development of most programs in Symbian C++, the use of co-operative multitasking (a.k.a. Active Objects) is preferred over pre-emptive multitasking (i.e. threads). When an asynchronous operation may take a long time however, it is probably a good idea to perform it in a separate thread. While it is possible to implement long-running operations using active objects, doing so may cause the application to become unresponsive, because the active scheduler will be unable to handle input events while executing the long-running RunL() function.

Similarly, in Qt, usage of threads is only required when your application needs to perform a long-running task. Event handling which would be handled using AOs in native Symbian code is taken care of by Qt's event system, with asynchronous notifications delivered via signals and slots. Unsurprisingly, the Qt event loop on Symbian is implemented using active objects.

So, while we can provide help on the usage of QThread, the question of whether this is the right solution depends on the nature of the problem you are trying to solve.

Gareth Stockwell