c++ - C++ 类中的错误 C2280 互斥体

标签 c++ class mutex

我在类中声明互斥锁时遇到问题,每当我尝试实例化该类时,都会出现错误 C2280。我的项目也是创建哲学家就餐问题的模拟。

声明:

class Fork
{

public:

struct _position
{
    float x;
    float y;
};

Fork();
Fork(int num_phil_, int new_index_);
_position ReturnWorkPosition(){ _position return_position_; /*position_mutex_.lock();*/ return_position_ = work_position_; 
                                    /*position_mutex_.unlock();*/ return return_position_; };
float ReturnRotation(){ return rotation_; };

void CalculateWorkPosition(int master_index_);
void ResetWorkPosition(){ /*position_mutex_.lock();*/ work_position_ = orig_position_; /*position_mutex_.unlock();*/ };
void TakeOwnership(int master_index_);
void RelinquishOwnership();

private:

int index_;
int num_philosophers_;
float rotation_;

_position orig_position_;
_position work_position_;

//std::mutex master_mutex_;
//std::mutex position_mutex_;
};

实例化:

for (int i = 0; i < num_philosophers_; i++)
{
    Fork temp_fork_(num_philosophers_, i);
    fork_.push_back(temp_fork_);
}

编辑::新声明:

class Fork
{

public:

Fork();
Fork(int num_phil_, int new_index_);

Fork(Fork const&) = delete;
Fork& operator=(Fork const&) = delete;

struct _position
{
    float x;
    float y;
};

_position ReturnWorkPosition(){ _position return_position_; /*position_mutex_.lock();*/ return_position_ = work_position_; 
                                    /*position_mutex_.unlock();*/ return return_position_; };
float ReturnRotation(){ return rotation_; };

void CalculateWorkPosition(int master_index_);
void ResetWorkPosition(){ /*position_mutex_.lock();*/ work_position_ = orig_position_; /*position_mutex_.unlock();*/ };
void TakeOwnership(int master_index_);
void RelinquishOwnership();

private:

int index_;
int num_philosophers_;
float rotation_;

_position orig_position_;
_position work_position_;

//std::mutex master_mutex_;
//std::mutex position_mutex_;
};

错误消息:

Error   18  error C2280: 'Fork &Fork::operator =(const Fork &)' : attempting to reference a deleted function    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 2045    1   Lab3e

Error   13  error C2280: 'Fork::Fork(const Fork &)' : attempting to reference a deleted function    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 600 1   Lab3e

新实例化:

for (int i = 0; i < num_philosophers_; i++)
{
    Fork temp_fork_(num_philosophers_, i);
    fork_.emplace_back(temp_fork_);
}

最佳答案

您需要删除复制构造函数和复制赋值运算符。您无法复制 std::mutex,因此您无法复制类的实例,只能构造或移动一个实例。

Fork(Fork const&) = delete;
Fork& operator=(Fork const&) = delete;

该错误告诉您正在尝试引用已删除的函数,即复制构造函数,因为 push_back 正在尝试复制您的 temp_fork_。相反,我会使用 emplace_back 就地创建 Fork

for (int i = 0; i < num_philosophers_; i++)
{
    fork_.emplace_back(num_philosophers_, i);
}

或者

for (int i = 0; i < num_philosophers_; i++)
{
    Fork temp_fork_(num_philosophers_, i);
    fork_.push_back(std::move(temp_fork_));
}

旁注
您可以使用 std::lock_guard 来执行此操作,而不是手动锁定和解锁 std::mutex ,这是一个设置用于通过 RAII 处理锁定/解锁互斥体的类

void ResetWorkPosition()
{
    std::lock_guard<std::mutex> lock(position_mutex_);  // position_mutex_ is now locked
    work_position_ = orig_position_;
};  // lock fell out of scope, position_mutex_ is now unlocked

_position ReturnWorkPosition()
{
    _position return_position_;
    std::lock_guard<std::mutex> lock(position_mutex_);  // position_mutex_ is now locked
    return_position_ = work_position_;
    return return_position_;
};

关于c++ - C++ 类中的错误 C2280 互斥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31465479/

相关文章:

c - 用互斥锁重新创建 sem_wait()?

c++ - 怎么可能锁定 GMutex 两次?

c++ - 想要在 Web 应用程序中使用 native C 库,我有哪些选择?

Java - JNA 和共享库,在 Linux 上从 .jar 启动时出现 UnsatisfiedLinkError

android - JNI OpenCV Android 画线或矩形功能

c++ - 无法在 C++ 中调用 const 引用参数的方法

linux - 是否有与 Windows 手动重置事件等效的 UNIX/pthreads?

c++ - 类的静态函数和类的构造函数是什么关系?

javascript - 如何在 JavaScript 中声明父类和子类?

c++ - 如何让一个类只能访问另一个类的某些私有(private)成员?