I have a thread class that works nice on desktop but crashes on android. In my Qt application I need a task with a shared object like this:
class UpdateTask : public QRunnable
{
MyPointer * _p;
void run()
{
qDebug() << "Hello world from thread" << QThread::currentThread();
_p.write();
qDebug() << "Hello3 world from thread" << QThread::currentThread();
}
public:
UpdateTask ();
~UpdateTask ();
void setPointer(MyPointer * pointer){
_p = pointer;
}
};
In main I want to be able to run the Task as follows:
UpdateTask * task = new UpdateTask ();
task->setPointer(_pointer);
QThreadPool::globalInstance()->start(task);
This works prefectly in desktop. But in android as you may know it does not work. when I run it Fatal signal 11 (SIGSEGV), code 1, fault addr 0x98 in tid 31727 (Thread (pooled))
occurs and only the first Hello prints before using the _p
So my question is this:
How Can I use MyPointer (a shared object) in all threads. It is not possible for me to pass a copy of it to each thread. It should be passed by pointer in all threads. In other words How could I use a shared object in all threads. In methods that are not const and each of the threads could change the object.
I know there are several techniques to handle multi-threded applications in Qt. which one is suitable for working on an android device?
Do I need to use JNI for safe multithreading in android? I guess I do!