简体   繁体   English

QT 4.3在线程之间传递类指针

[英]QT 4.3 passing class pointer between threads

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 4.3.1(必须使用传统代码),并且正在使用信号和插槽在工作线程和GUI线程之间传递类引用。

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

I have class, COMinfo made from another class and a struct using native types: 我有一个类,COMinfo由另一个类和使用本机类型的结构构成:

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");

I registered the data type to the MOC with qRegisterMetaType(). 我使用qRegisterMetaType()向MOC注册了数据类型。

I am passing references between my worker thread and the GUI thread like so: 我在工作线程和GUI线程之间传递引用,如下所示:

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

What does QT with this reference? QT与此参考有什么关系? Is it a shallow copy? 它是浅表吗? Need a copy constructor? 需要复制构造函数吗?

The signal is defined as: 该信号定义为:

void   COM_DATA_UPDATE(const U_BYTE* msg);

Also have a signal I pass the actual const class reference: 也有一个信号我通过了实际的const类引用:

void   INFO_UPDATE(const COMInfo& comInfo);

"Does QT perform atomic operations on this class if it is composed of native types" - the language is C++, Qt just a library. “如果QT由本机类型组成,则QT会对此类进行原子操作吗?”-语言是C ++,Qt只是一个库。 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. 因此,当从多个线程访问变量并且应用通常的C ++多线程规则及其内存模型(自C ++ 11起)时,Qt不会发挥任何作用。 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. 但是,信号/插槽机制实现了线程间通信的消息传递机制: 排队连接 (另请参阅线程同步页面)通过复制参数将参数从一个线程传递到另一个线程。 Ie, 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. 即,传递诸如QString,int,double之类的值类型是安全的-接收线程将对发送线程数据的副本进行操作,并且没有需要锁定的并发访问。 Signal/Slot arguments passed by const reference will also be copied by the signal/slot system. const引用传递的Signal / Slot参数也将被signal / slot系统复制。

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). 但是,如果将指针作为参数传递或包含在结构中,则该指针将被复制 ,而不是其指向的对象(在这种情况下,没有自定义复制ctor / assignment运算符的实现)。 Thus, accessing the passed pointer is not safe for the and you'd need a synchronisation mechanism such as a mutex. 因此,对于而言,访问传递的指针并不安全,并且您需要同步机制(例如互斥体)。

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) . 在您的示例中, PORT_CHECK_STRUCT *portCheck出现此问题(在传递PortInfo对象时),以及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). 我建议通过std :: array(或std :: vector / QVector如果您不能使用C ++ 11)传递数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM