Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 214
Registered: ‎03-18-2012
My Device: 8520
My Carrier: Company

In BB10 how to do multithreading

Hi ,

 

please suggest how we do multithreading in blackberry 10.

Developer
Posts: 217
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: In BB10 how to do multithreading

Highlighted
Contributor
Posts: 40
Registered: ‎05-14-2013
My Device: Z10
My Carrier: none

Re: In BB10 how to do multithreading

Developer
Posts: 214
Registered: ‎03-18-2012
My Device: 8520
My Carrier: Company

Re: In BB10 how to do multithreading

Hi, 

 

I have  made two threads using the approch as given in the below url.

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/The-Recommended-Way-to-Use-QTh...

 

But they are not working correctly.

problem i face is that  in main class i start 2  threads and  jsut for  few seconds first thread run and after it second thread start execution and always remain in running state means first thread donot do any exceution(never get cpu for the execution). 

 

Is there any approach

Contributor
Posts: 40
Registered: ‎05-14-2013
My Device: Z10
My Carrier: none

Re: In BB10 how to do multithreading

Did you call run() instead of start() ?

New Developer
Posts: 25
Registered: ‎01-23-2013
My Device: BB10 Simulator
My Carrier: Callmobile - T-Mobile

Re: In BB10 how to do multithreading

How do you "start execution" of the second thread? If you just use QThread::start(), it should not block anything.

 

The QThread::start() method just starts the event loop inside the thread without blocking the current thread.

 

Example:

object1->moveToThread( thread1 );

object2->moveToThread( thread2 );

thread1->start();

thread2->start();

 

Now, be aware that the methods are not magically running on the object thread, any normal method call is blocking. Only the methods triggered from the event loop are running inside the thread! To make sure that a given method of an object is running in its event thread, it must either be a slot triggered after a signal has been emitted, or using QMetaObject::invokeMethod (providing that the method is declared as slot or with Q_INVOKABLE)

 

Example:

object2->method2();  // blocking method executed in the current thread

QMetaObject::invokeMethod( object2, "method2" );  // non-blocking, call posted to the event-loop of thread2

 

All connected slots are automatically running on the object thread, for example if you start a method using QTimer::singleShot( 0, SLOT( ... ) ).

 

If you use QMetaObject::invokeMethod(), I recommand reading the documentation carefully (especially when using return values).

Developer
Posts: 214
Registered: ‎03-18-2012
My Device: 8520
My Carrier: Company

Re: In BB10 how to do multithreading

Hi ,

 

I have use thread->start();

New Developer
Posts: 25
Registered: ‎01-23-2013
My Device: BB10 Simulator
My Carrier: Callmobile - T-Mobile

Re: In BB10 how to do multithreading

Did you create a bb::cascades::Application (or a QApplication) instance before? Please send also the source code, that would help to help you.

Developer
Posts: 214
Registered: ‎03-18-2012
My Device: 8520
My Carrier: Company

Re: In BB10 how to do multithreading

Hi,

 

please find.

 

#include <QLocale>
#include <QTranslator>
#include <Qt/qdeclarativedebug.h>
#include "QThreadTest.hpp"
#include <qthread.h>
using namespace bb::cascades;
void myMessageOutput(QtMsgType type, const char* msg)
{
           fprintf(stdout, "%s\n", msg);
           fflush(stdout);
}
Q_DECL_EXPORT int main(int argc, char **argv)
{
    // this is where the server is started etc
    Application app(argc, argv);
    qInstallMsgHandler(myMessageOutput);
    // localization support
    QTranslator translator;
    QString locale_string = QLocale().name();
    QString filename = QString( "QThreadTest_%1" ).arg( locale_string );
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator( &translator );
    }

    QThread *th1 = new QThread();

    QThread *th2 = new QThread();

    QThreadTest* thread = new QThreadTest("Ashish***");
    QThreadTest* thread1 = new QThreadTest("Sharma---");
    thread->moveToThread(th1);
    thread1->moveToThread(th2);

    QObject::connect(th1, SIGNAL(started()), thread, SLOT(process()));
    th1->start();

    QObject::connect(th2, SIGNAL(started()), thread1, SLOT(process()));
        th2->start();
    // we complete the transaction started in the app constructor and start the client event loop here
    return Application::exec();
    // when loop is exited the Application deletes the scene which deletes all its children (per qt rules for children)
}


// Default empty project template
#ifndef QThreadTest_HPP_
#define QThreadTest_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class QThreadTest : public QObject
{
    Q_OBJECT
public:
    QThreadTest(QString);
    virtual ~QThreadTest() {}
public slots:
void process();
private:
QString text;
};


#endif /* QThreadTest_HPP_ */


// Default empty project template
#include "QThreadTest.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

QThreadTest::QThreadTest(QString text)
{
	this->text = text;
}

void QThreadTest::process()
{
   while(true)
   {
	   qDebug()<<text;
   }

}

 

New Developer
Posts: 25
Registered: ‎01-23-2013
My Device: BB10 Simulator
My Carrier: Callmobile - T-Mobile

Re: In BB10 how to do multithreading

[ Edited ]

Try adding QThread::yieldCurrentThread() into the loop of QThreadTest:Smiley Tonguerocess(), just after the qDebug() call.

 

UPDATE: As a (better) alternative, use QTimer::singleShot( 1, SLOT( process() ) ) instead of having a while loop