11-23-2012 04:02 AM
I've followed the recommended use of QThread (http://supportforums.blackberry.com/t5/Cascades-De
My app is trying to run threads, each thread runs an http request. I found I was receiving crashes after about 120 threads had been run so I created the above much simplified example to try and pinpoint my error.
I've created the below simple example that is (I think) failing to cleanly remove the old threads or worker instances, finally crashing.
My main app constructor creates a thread; this thread just contains an infinite loop chucking out a signal every 10th of a second, the slot that catches this signal in also in my main app. This bit is very simple, not the problem (I think).
Proj2::Proj2(bb::cascades::Application *app)
: QObject(app)
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
root = qml->createRootObject<AbstractPane>();
app->setScene(root);
QThread* thread = new QThread;
MainThread* worker = new MainThread();
worker->moveToThread(thread);
// connect thread started() to the process slot in the worker
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(responded()), this, SLOT(myslot()));
thread->start();
}
So the slot in my main app gets called 10 times a second and contains the code:
void Proj2::myslot()
{
QThread* thrd = new QThread;
MyWorker* worker = new MyWorker();
worker->moveToThread(thrd);
connect(thrd, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), thrd, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thrd, SIGNAL(finished()), thrd, SLOT(deleteLater()));
thrd->start();
}
And the MyWorker class is almost identical to the code in knoledge base example. When it starts, it calls process() which is just:
#include "MyWorker.h"
#include <stdio.h>
#include <unistd.h>
MyWorker::MyWorker() {
}
MyWorker::~MyWorker() {
}
void MyWorker::process()
{
emit finished();
}
If I run it, after 10 seconds or so I get the error:
QEventDispatcherUNIXPrivate(): Unable to create thread pipe
Too many open files
If anyone could advice I'd really appreciate it!
11-26-2012 03:47 AM
anyone? ![]()
11-27-2012 10:15 AM
got similar issue with the example
resoved by:
1. disable
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
2. added to the worker finished() slot:
thread->quit(); thread->wait();
11-27-2012 10:27 AM
hi vladest
many thanks for your help ,really appreciate it.
Step 2 was to add
thread->quit(); thread->wait();
I didn't fully understand...right now, in my worker thread, in Process() I emit a finished signal. I don't know about the thread itself in the worker class.
Should I pass a pointer to the thread into the worker class and quite/wait in the worker class? (instead of emitting a finished signal)
Or should I emit a finished signal and in my main class catch the signal and somehow get the thread info?
Apologies for my ignorance
11-27-2012 10:30 AM
For your particular requirements, I wonder if QThreadPool might be a better fit?
http://doc.qt.digia.com/4.6/qthreadpool.html
11-27-2012 10:30 AM
yes, you right
finished() slot in main class, having both worker and thread pointers
11-27-2012 11:15 AM
hi vladest
nearly there
again, huge thanks.
When I connect signal and slot, can I pass the thread here?
Something like:
void Proj2::threadfinished(QThread *thread)
{
printf("threadfinished\n");
fflush(stdout);
thread->quit();
thread->wait();
}
QThread* thrd = new QThread;
MyWorker* worker = new MyWorker();
worker->moveToThread(thrd);
....
connect(worker, SIGNAL(finished()), this, SLOT(threadfinished(thrd)));It compiles fine but the slot isn't called. If I stop trying to pass the thread across, it works.
I'm unsure how to pass the thread info to the slot
thanks
11-27-2012 11:18 AM
hi
it doesnt makes sense to pass thread to the slot cause this (ie main class pointer) _already_ has the thread pointer