QT 4.3在线程之间传递类指针 [英] QT 4.3 passing class pointer between threads

查看:1275
本文介绍了QT 4.3在线程之间传递类指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QT 4.3.1(必须使用传统代码),并且正在使用信号和插槽在工作线程和GUI线程之间传递类引用.

I am using QT 4.3.1 (Legacy code have to use it) and I am passing a class reference between my worker thread and the GUI thread using signals and slots.

如果QT由本机类型(Qstring,int,unsigned char)组成,则QT对该类执行原子操作吗?还是我仍然需要QMutex?在线程之间传递类或数据缓冲区的引用是否安全,还是有更好的共享方式并防止并发访问?

Does QT perform atomic operations on this class if it is composed of native types (Qstring, int, unsigned char), or do I still need a QMutex? Does passing a reference of the class or data buffer between threads safe, or is there a better way to share it and prevent concurrent access?

我有一个类,它是由另一个类制成的COMinfo和一个使用本机类型的结构:

I have class, COMinfo made from another class and a struct using native types:

typedef struct Port_Check_Struct
{

    U_BYTE  command       :4;
    U_BYTE  spare15_4   :2; /* spare bits */
    U_BYTE  status       :2; 

} PORT_CHECK_STRUCT;


class PortInfo
{
public:

    PortInfo();

    QString     portName;
    U_BYTE      Dir;
    UNSIGNED16  Addr;
    U_BYTE      Size;

    bool        active;
    U_BYTE      lastused;
    U_BYTE      currused;
    U_BYTE      errorCntr;

    U_BYTE      portBuff[100];
    U_BYTE      dataBuff[100];

    PORT_CHECK_STRUCT   *portCheck;
};


class COMInfo
{
public:
    UNSIGNED8   errorStatus;

    UNSIGNED16  COMaddr;

    PortInfo    portInfo[4];
};

    // Define Meta Types for SIGNAL/SLOT CROSS THREAD TYPES
    qRegisterMetaType<COMInfo>("COMInfo");

我使用qRegisterMetaType()向MOC注册了数据类型.

I registered the data type to the MOC with qRegisterMetaType().

我在工作线程和GUI线程之间传递引用,如下所示:

I am passing references between my worker thread and the GUI thread like so:

emit COM_DATA_UPDATE(COM_Info.portInfo[0].dataBuff)

此参考书对QT有什么帮助?它是浅表吗?需要复制构造函数吗?

What does QT with this reference? Is it a shallow copy? Need a copy constructor?

信号定义为:

void   COM_DATA_UPDATE(const U_BYTE* msg);

还有一个信号,我通过了实际的const类引用:

Also have a signal I pass the actual const class reference:

void   INFO_UPDATE(const COMInfo& comInfo);

推荐答案

如果QT由本机类型组成,则QT会对此类进行原子操作吗?"-语言是C ++,Qt只是一个库.因此,当从多个线程访问变量并且应用通常的C ++多线程规则及其内存模型(自C ++ 11起)时,Qt不会发挥任何作用.但是,信号/插槽机制实现了用于线程间通信的消息传递机制:线程同步),通过复制将参数从一个线程传递到另一个线程.也就是说,传递诸如QString,int,double之类的值类型是安全的-接收线程将对发送线程数据的副本进行操作,并且没有需要锁定的并发访问. const引用传递的Signal/Slot参数也将被signal/slot系统复制.

"Does QT perform atomic operations on this class if it is composed of native types" - the language is C++, Qt just a library. Thus Qt doesn't do any magic when variables are accessed from multiple threads and the usual C++ multithreading rules and its memory model (since C++11) apply. The signal/slot mechanism however implements a message passing mechanism for inter-thread communication: Queued connections (see also the page on thread synchronisation) pass the arguments from one thread to another by copying them. I.e., passing value types such as QString, int, double is safe - the receiving thread will operate on a copy of the sending thread's data and there's no concurrent access that'd need locking. Signal/Slot arguments passed by const reference will also be copied by the signal/slot system.

但是,如果您将指针作为参数传递或包含在结构中,则指针将被复制,而不是其指向的对象(在这种情况下,没有自定义复制ctor/assignment运算符实现否则).因此,访问传递的指针对于 是不安全的,并且您需要使用同步机制,例如互斥锁.

However, if you pass pointers either as argument or contained in a struct, the pointer is copied, not the object it points to (in the case there's no custom copy ctor/assignment operator implementation doing otherwise). Thus, accessing the passed pointer is not safe for the and you'd need a synchronisation mechanism such as a mutex.

在您的示例中,PORT_CHECK_STRUCT *portCheck以及void COM_DATA_UPDATE(const U_BYTE* msg)都有此问题(在传递PortInfo对象时).我建议通过std :: array(或如果无法使用C ++ 11的话,可以通过std :: vector/QVector)传递数据.

In your example, PORT_CHECK_STRUCT *portCheck has this problem (when passing a PortInfo object), as well as void COM_DATA_UPDATE(const U_BYTE* msg). I'd suggest to pass the data via std::array (or std::vector/QVector if you can't use C++11).

这篇关于QT 4.3在线程之间传递类指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆