C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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);
}

@L_801_20@

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 – 如何在类上下文中使用std :: mutex全部内容,希望文章能够帮你解决c – 如何在类上下文中使用std :: mutex所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。