如何在类上下文中使用std :: mutex
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在类上下文中使用std :: mutex相关的知识,希望对你有一定的参考价值。
我在使用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);
}
答案
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;
}
};
另一答案
动态声明您的互斥锁而不是静态声明。
在您的Database类头中,将互斥锁声明为指针,如下所示:
mutex * semaphore;
并在您的构造函数初始化调用其构造函数的互斥锁:
semaphore = new std::mutex();
以上是关于如何在类上下文中使用std :: mutex的主要内容,如果未能解决你的问题,请参考以下文章
std::mutex 锁定函数和 std::lock_guard<std::mutex> 的区别?
将 std::thread 与 std::mutex 一起使用
排序多个线程如何在 std::condition_variable::notify_all 之后重新获取 std::unique_lock<std::mutex>