我正在使用Qt5构建应用程序。我的程序构建并运行良好,但是访问数据结构的两个线程之间存在冲突。我有一个CanMessage对象的QList,我想使用QMutex保护其中的一些数据。但是,一旦我将QMutex添加到类定义中,就会收到错误消息:

QList.h: `error: C2280:
'CanMessage::CanMessage(const CanMessage &)': attempting to reference a deleted function`.

这是我的canmessage.h文件:
#ifndef CANMESSAGE_H
#define CANMESSAGE_H

#include <QObject>
#include <QMutex>
#include "cansignal.h"

class CanMessage
{
public:
    CanMessage();
    /* snip - public function prototypes */

private:
    /* snip - private prototypes and data */
    QMutex m_messageMutex;
};

#endif // CANMESSAGE_H

cansignal.h:
#ifndef CANSIGNAL_H
#define CANSIGNAL_H

#include <QObject>
#include <QDebug>
#include <QByteArray>

class CanSignal
{
public:
    CanSignal();

    CanSignal(QString &signalName, quint8 &signalLength, quint8 &signalStartBit,
                   float &offset, float &factor, bool &isBigEndian, bool &isFloat, bool &isSigned)
    {
        this->m_name = signalName;
        this->m_length = signalLength;
        this->m_startBit = signalStartBit;
        this->m_offset = offset;
        this->m_factor = factor;
        this->m_isBigEndian = isBigEndian;
        this->m_isFloat = isFloat;
        this->m_isSigned = isSigned;
    }

    bool setName(QString &signalName);
    bool setBitLength(quint8 &length);
    bool setStartBit(quint8 &startBit);
    bool setOffset(float &offset);
    bool setFactor(float &factor);
    bool setEndianess(bool &isBigEndian);
    bool setIsFloat(bool &isFloat);
    bool setIsSigned(bool &isSigned);
    void setValid();
    void setInvalid();
    void setEngineeringData(float data);

    QString getName();
    quint8 getBitLength();
    quint8 getStartBit();
    float getOffset();
    float getFactor();
    float getData();
    bool isBigEndian();
    bool isFloat();
    bool isSigned();
    bool getSignalValidity();


private:
    QString m_name;
    quint8 m_length;
    quint8 m_startBit;
    float m_offset;
    float m_factor;
    float m_data_float = 0;
    bool m_isBigEndian;
    bool m_isFloat;
    bool m_isSigned;
    // Set After everything in signal is filled
    bool m_isSignalValid = false;
};

#endif // CANSIGNAL_H

最佳答案

CanMessage::CanMessage(const CanMessage &)

是复制构造函数,显然用于将项目放置到列表中。这是行不通的,因为QMutex实际上不可复制。

您如何解决它取决于许多因素。也许最简单的方法是修改CanMessage,使其具有动态互斥体(当然是在构造函数中创建的)。

然后为其提供一个复制构造函数,该构造函数首先锁定源对象互斥锁,然后在目标对象中动态分配新的互斥锁。

这样,您可以保证在复制时旧对象将是“干净的”(因为您具有其互斥锁),并且不会出现“试图复制不可复制成员”的问题,因为互斥锁本身不会被复制。有关详细信息,请参见脚注(a)

以下代码是显示问题的完整简单代码段,只要您将QMutex m_mutex;行留为注释,就可以正常编译:
#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
    int m_xyzzy;
    //QMutex m_mutex;
public:
    Xyzzy() : m_xyzzy(0) {};
    Xyzzy(int val) : m_xyzzy(val) {};
};

int main() {
    QList<Xyzzy> myList;
    Xyzzy plugh;
    myList.push_back(plugh);
    return 0;
}

一旦取消注释该行,编译器便会正确地抱怨:
 error: use of deleted function 'Xyzzy::Xyzzy(const Xyzzy&)'

(a)就解决问题而言,您可以执行以下操作:
#include <QList>
#include <QMutex>

#include <iostream>

class Xyzzy {
private:
    int m_xyzzy;
    QMutex *m_mutex; // Now a pointer
public:
    Xyzzy() : m_xyzzy(0) {
        m_mutex = new QMutex(); // Need to create in constructor.
        std::cout << "constructor " << m_mutex << '\n';
    };
    ~Xyzzy() {
        std::cout << "destructor " << m_mutex << '\n';
        delete m_mutex; // Need to delete in destructor.
    }
    Xyzzy(const Xyzzy &old) {
        old.m_mutex->lock();
        m_mutex = new QMutex(); // Need to make new one here.
        std::cout << "copy constructor from " << old.m_mutex
                  << " to " << m_mutex << '\n';
        old.m_mutex->unlock();
    }
};

int main() {
    QList<Xyzzy> myList;
    Xyzzy plugh;
    myList.push_back(plugh);
    return 0;
}

根据以下测试运行,该程序可以正常工作:
constructor 0x21c9e50
copy constructor from 0x21c9e50 to 0x2fff2f0
destructor 0x21c9e50
destructor 0x2fff2f0

在实际代码中,我可能会选择智能指针,而不是原始的new/delete调用,但这仅是为了说明概念。此外,您还需要处理所有其他可能性,这些可能性将按照现有的3/5 / whatever-comes-next的规则从现有对象创建一个新对象(当前(从内存中))限制为副本分配成员Xyzzy &operator=(const Xyzzy &old)

07-25 20:25