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

前端之家收集整理的这篇文章主要介绍了c – 如何在类上下文中使用std :: mutex前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用C 11 std :: mutex时遇到了一些麻烦

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

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

class Database {
public:
Database(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(),NULL,0);
}

解决方法

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,std::adopt_lock);
    value = other.value;
    return *this;
  }
};

猜你在找的C&C++相关文章