如何在类上下文中使用std :: mutex

问题描述 投票:6回答:2

我在使用C ++ 11 std :: mutex时遇到了一些麻烦

在我的课堂上,我有一个名为std :: mutex的信号量变量。

所以我在我的关键部分之前和之后定位了我的semaphore.lock()和semaphore.unlock()

class Database {
public:
Database(string, string, string, string);
virtual ~Database();

struct sMyHome getMyHome(void);
struct sPhysical getPhysical(int);
struct sSystemInfo getSystemInfo(void);
void commitSystemInfo(struct sSystemInfo);
struct sTSensors getTSensors(int);
struct sWireless getWireless(int);
struct sWirelessConf getWirelessConf(int);
struct sWirelessStatus getWirelessStatus(int);

private:
void queryMyHome(void);
void queryPhysical(int);
void querySystemInfo(void);
void queryTSensors(int ID);
void queryWireless(int ID);
void queryWirelessConf(int ID);
void queryWirelessStatus(int ID);
string toString(int);

struct sMyHome MyHome;
struct sPhysical Physical[4];
struct sSystemInfo SystemInfo;
struct sTSensors TSensors[32];
struct sWireless Wireless[64];
struct sWirelessConf WirelessConf[64];
struct sWirelessStatus WirelessStatus[64];

MYSQL *connection;
mutex semaphore;
};

这是main的一部分,我在其中检索错误:

 Database db = Database("192.168.1.10", "****", "******", "******");

但是交叉编译器说

../main.cpp:8:73: error: use of deleted function 'Database::Database(const Database&)'
  Database db = Database("192.168.1.10", "root", "raspberry", "DomoHome2");
                                                                     ^
In file included from ../main.cpp:2:0:
../Database.h:79:7: note: 'Database::Database(const Database&)' is implicitly deleted      because the default definition would be ill-formed:
 class Database {
   ^
../Database.h:79:7: error: use of deleted function 'std::mutex::mutex(const std::mutex&)'
 In file included from ../Database.h:12:0,
             from ../main.cpp:2:
/Volumes/rpi-crosscompiler-toolchain/arm-none-linux-gnueabi/arm-none-linux-gnueabi/include/c++/4.8.2/mutex:128:5: error: declared here
 mutex(const mutex&) = delete;
 ^
make: *** [main.o] Error 1

这是我的构造函数:

Database::Database(string Address, string Username, string Password, string Database) {
// TODO Auto-generated constructor stub
connection = mysql_init(NULL);
mysql_real_connect(connection, Address.c_str(), Username.c_str(), Password.c_str(), Database.c_str(), 0, NULL, 0);
}
c++ multithreading c++11 mutex cross-compiling
2个回答
14
投票

std::mutex既不是可复制的也不是可移动的。在您的班级中加入一个必须使您的班级变得不可复制( - 可移动)。如果您希望您的类可以复制或移动,则必须通过自己实现复制/移动构造/赋值来告诉编译器如何复制或移动类的对象。 For example

class synchronized_int {
  mutable std::mutex mtx;
  int value;
public:
  synchronized_int(int v = 0) : value(v) {}

  // Move initialization
  synchronized_int(synchronized_int&& other) {
    std::lock_guard<std::mutex> lock(other.mtx);
    value = std::move(other.value);
    other.value = 0;
  }

  // Copy initialization
  synchronized_int(const synchronized_int& other) {
    std::lock_guard<std::mutex> lock(other.mtx);
    value = other.value;
  }

  // Move assignment
  synchronized_int& operator = (synchronized_int&& other) {
    std::lock(mtx, other.mtx);
    std::lock_guard<std::mutex> self_lock(mtx, std::adopt_lock);
    std::lock_guard<std::mutex> other_lock(other.mtx, std::adopt_lock);
    value = std::move(other.value);
    other.value = 0;
    return *this;
  }

  // Copy assignment
  synchronized_int& operator = (const synchronized_int& other) {
    std::lock(mtx, other.mtx);
    std::lock_guard<std::mutex> self_lock(mtx, std::adopt_lock);
    std::lock_guard<std::mutex> other_lock(other.mtx, std::adopt_lock);
    value = other.value;
    return *this;
  }
};

1
投票

动态声明您的互斥锁而不是静态声明。

在您的Database类头中,将互斥锁声明为指针,如下所示:

mutex * semaphore;

并在您的构造函数初始化调用其构造函数的互斥锁:

semaphore = new std::mutex();
© www.soinside.com 2019 - 2024. All rights reserved.