Qt Android,如何访问QRunnable中的外部对象?

Qt Android,如何访问QRunnable中的外部对象?,android,multithreading,qt,qthread,qtandroidextras,Android,Multithreading,Qt,Qthread,Qtandroidextras,我有一个线程类,在桌面上运行良好,但在android上崩溃。在我的Qt应用程序中,我需要一个具有如下共享对象的任务: class UpdateTask : public QRunnable { MyPointer * _p; void run() { qDebug() << "Hello world from thread" << QThread::currentThread(); _p.write();

我有一个线程类,在桌面上运行良好,但在android上崩溃。在我的Qt应用程序中,我需要一个具有如下共享对象的任务:

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;
    }
};
这在桌面上非常有效。但在安卓系统中,你可能知道它不起作用。当我运行tid 31727(线程(池化))中的致命信号11(SIGSEGV)、代码1、故障地址0x98时,会发生,并且在使用前只打印第一个Hello 所以我的问题是:
如何在所有线程中使用MyPointer(共享对象)。我不可能将它的副本传递给每个线程。它应该在所有线程中通过指针传递。换句话说,如何在所有线程中使用共享对象。在非常量的方法中,每个线程都可以更改对象。
我知道Qt中有几种处理多线程应用程序的技术。哪一个适合在android设备上工作?

我需要在android中使用JNI来实现安全的多线程吗?我想是的

通过使用互斥锁、信号量或其他东西包装对指针的访问,使其线程安全

另一种方法是使用排队信号插槽连接发送

这里有一种使用互斥锁的方法:

// Member variable of UpdateTask
QMutex m_mutex;
// In your constructor
_p = 0;

void UpdateTask::setPointer(MyPointer *pointer)
{
    QMutexLocker locker(&m_mutex);
    _p = pointer;
}

void UpdateTask::run()
{
    // Create connections here, and the thread affinity will be correct, 
    // otherwise you need to use moveToThread() or explicitly say a 
    // Qt::QueuedConnection


    // Any place where _p is accessed
    {
        QMutexLocker locker(&m_mutex);
        if(p != 0)
            p->write();
    }
}


希望能有所帮助。

谢谢你的回答。但是我在桌面上测试了这个应用程序,它在没有互斥的情况下工作得很好。问题在于android。互斥锁没有解决这个问题。
// Member variable of UpdateTask
QMutex m_mutex;
// In your constructor
_p = 0;

void UpdateTask::setPointer(MyPointer *pointer)
{
    QMutexLocker locker(&m_mutex);
    _p = pointer;
}

void UpdateTask::run()
{
    // Create connections here, and the thread affinity will be correct, 
    // otherwise you need to use moveToThread() or explicitly say a 
    // Qt::QueuedConnection


    // Any place where _p is accessed
    {
        QMutexLocker locker(&m_mutex);
        if(p != 0)
            p->write();
    }
}