gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:15:54 29 4
gpt4 key购买 nike

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

在我的类(class)中,我有一个名为 semaphore 的变量,类型为 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;
};

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

 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);
}

最佳答案

std::mutex 既不可复制也不可移动。在你的类(class)中加入一个类(class)需要你的类(class)也变得不可复制(-可移动)。如果您希望您的类是可复制或可移动的,则必须通过自己实现复制/移动构造/赋值来告诉编译器如何复制或移动您的类的对象。 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;
}
};

关于c++ - 如何在类上下文中使用 std::mutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272641/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com